Я реализую вход в Facebook и Google в Android Studio.Данные сохраняются в базе данных, но в ответ в API я получаю результат false .Я новичок в программировании, но я провел исследование, но все еще не могу пройти через него.Я реализовал следующим образом ...
Моя функция PHP:
public function facebookViaAPI(Request $request)
{
$validator = Validator::make(
$request->all(),
[
'device_type' => 'required|in:android,ios',
'device_token' => 'required',
'accessToken' => 'required',
//'mobile' => 'required',
'device_id' => 'required',
'login_by' => 'required|in:manual,facebook,google'
]
);
if ($validator->fails()) {
return response()->json(['status' => false, 'message' => $validator->messages()->all()]);
}
$user = Socialite::driver('facebook')->stateless();
$FacebookDrive = $user->userFromToken($request->accessToken);
try {
$FacebookSql = User::where('social_unique_id', $FacebookDrive->id);
if ($FacebookDrive->email != "") {
$FacebookSql->orWhere('email', $FacebookDrive->email);
}
$AuthUser = $FacebookSql->first();
if ($AuthUser) {
$AuthUser->social_unique_id = $FacebookDrive->id;
$AuthUser->device_type = $request->device_type;
$AuthUser->device_token = $request->device_token;
$AuthUser->device_id = $request->device_id;
$AuthUser->mobile = $request->mobile ?: '';
$AuthUser->login_by = "facebook";
$AuthUser->save();
} else {
$AuthUser = new User();
$AuthUser->email = $FacebookDrive->email;
$name = explode(' ', $FacebookDrive->name, 2);
$AuthUser->first_name = $name[0];
$AuthUser->last_name = isset($name[1]) ? $name[1] : '';
$AuthUser->password = bcrypt($FacebookDrive->id);
$AuthUser->social_unique_id = $FacebookDrive->id;
$AuthUser->device_type = $request->device_type;
$AuthUser->device_token = $request->device_token;
$AuthUser->device_id = $request->device_id;
$AuthUser->mobile = $request->mobile ?: '';
$AuthUser->picture = $FacebookDrive->avatar;
$AuthUser->login_by = "facebook";
$AuthUser->save();
}
if ($AuthUser) {
$userToken = $AuthUser->token() ?: $AuthUser->createToken('socialLogin');
return response()->json([
"status" => true,
"token_type" => "Bearer",
"access_token" => $userToken->accessToken
]);
} else {
return response()->json(['status' => false, 'message' => "Invalid credentials!"]);
}
} catch (Exception $e) {
return response()->json(['status' => false, 'message' => $e->getMessage() . "\n" . $e->getTraceAsString()]);
}
}
Мой код для Android:
public void login(final String accesstoken, final String URL, final String Loginby) {
Log.d("Login Enter","::::::");
customDialog = new CustomDialog(context);
customDialog.setCancelable(false);
if (customDialog != null)
customDialog.show();
final JsonObject json = new JsonObject();
json.addProperty("device_type", "android");
json.addProperty("device_token", device_token);
json.addProperty("accessToken", accesstoken);
json.addProperty("device_id", device_UDID);
json.addProperty("login_by", Loginby);
json.addProperty("mobile",mobileNumber);
Log.e(TAG, "login: Facebook" + json);
Ion.with(ActivitySocialLogin.this)
.load(URL)
.addHeader("X-Requested-With", "XMLHttpRequest")
.addHeader("Authorization","Bearer"+SharedHelper.getKey(context, "token_type")+" "+SharedHelper.getKey(context, "access_token"))
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
// Log.d("Completed Enter" , "::::::::"+result.toString());
Log.d("Exception" , "::::::::"+e);
Log.d("Completed Enter result" , "::::::::"+result);
if ((customDialog != null) && customDialog.isShowing())
customDialog.dismiss();
if (e != null) {
if (e instanceof NetworkErrorException) {
displayMessage(getString(R.string.oops_connect_your_internet));
} else if (e instanceof TimeoutException) {
login(accesstoken, URL, Loginby);
} else{
Log.d("Access Fail" , "::::::::");
displayMessage(getString(R.string.please_try_again));
}
return;
}
if (result != null) {
Log.v(Loginby + "_Response", result.toString());
Log.d("Response" + "_Response", result.toString());
try {
JSONObject jsonObject = new JSONObject(result.toString());
String status = jsonObject.optString("status");
if (status.equalsIgnoreCase("true")) {
SharedHelper.putKey(ActivitySocialLogin.this, "token_type", jsonObject.optString("token_type"));
SharedHelper.putKey(ActivitySocialLogin.this, "access_token", jsonObject.optString("access_token"));
if (Loginby.equalsIgnoreCase("facebook"))
SharedHelper.putKey(ActivitySocialLogin.this, "login_by", "facebook");
if (Loginby.equalsIgnoreCase("google"))
SharedHelper.putKey(ActivitySocialLogin.this, "login_by", "google");
if (!jsonObject.optString("currency").equalsIgnoreCase("") && jsonObject.optString("currency") != null)
SharedHelper.putKey(context, "currency", jsonObject.optString("currency"));
else
SharedHelper.putKey(context, "currency", "$");
//phoneLogin();
getProfile();
} else {
GoToBeginActivity();
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}else {
Log.d(" Login Failure" , "::::::::");
displayMessage(getString(R.string.please_try_again));
}
}
});
}
Мой журнал распечатать ответ Exception e и результат выглядит следующим образом:
Exception: ::::::::null
Response_Response: {"status":false,"message":"Trying to get property of non-object\n#0 /home/aayocab/public_html/aayocab/vendor/laravel/passport/src/ClientRepository.php(66): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/home/aayocab/p...', 66, Array)\n#1 /home/aayocab/public_html/aayocab/vendor/laravel/passport/src/PersonalAccessTokenFactory.php(71): Laravel\\Passport\\ClientRepository->personalAccessClient()\n#2 /home/aayocab/public_html/aayocab/vendor/laravel/passport/src/HasApiTokens.php(67): Laravel\\Passport\\PersonalAccessTokenFactory->make(74, 'socialLogin', Array)\n#3 /home/aayocab/public_html/aayocab/app/Http/Controllers/Auth/SocialLoginController.php(171): App\\User->createToken('socialLogin')\n#4 [internal function]: App\\Http\\Controllers\\Auth\\SocialLoginController->facebookViaAPI(Object(Illuminate\\Http\\Request))\n#5 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)\n#6 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\\Routing\\Controller->callAction('facebookViaAPI', Array)\n#7 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Route.php(189): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\Auth\\SocialLoginController), 'facebookViaAPI')\n#8 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Route.php(144): Illuminate\\Routing\\Route->runController()\n#9 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Router.php(653): Illuminate\\Routing\\Route->run(Object(Illuminate\\Http\\Request))\n#10 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#11 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#12 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#13 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#14 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(49): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#15 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), '60', '1')\n#16 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#17 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#18 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Router.php(655): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))\n#19 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Router.php(629): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))\n#20 /home/aayocab/public_html/aayocab/vendor/laravel/framework/src/Illuminate/Routing/Router.php(607): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))\n#21 /h