Example #1
0
 /**
  * @param Request $request
  * @return \BladeView|bool|\Illuminate\View\View|mixed|\Symfony\Component\HttpFoundation\Response
  */
 public function addFacebookAccount(Request $request)
 {
     $util = new Util();
     $userService = new userService();
     $accessToken = $request->input('accessToken', '');
     $result['ok'] = 0;
     $result['accessToken'] = $accessToken;
     try {
         $user = Auth::user();
         $facebook = $util->initFacebook();
         // OAuth 2.0 client handler
         $oAuth2Client = $facebook->getOAuth2Client();
         // Exchanges a short-lived access token for a long-lived one
         $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
         $facebook->setDefaultAccessToken($longLivedAccessToken);
         $profile = $facebook->getProfile();
         $result = $userService->createBusinessSocialAccount($profile, $longLivedAccessToken, $user);
     } catch (\Facebook\Exceptions\FacebookResponseException $e) {
         if ($request->ajax()) {
             $result['error_message'] = $e->getMessage();
             return $result;
         } else {
             return view('errors.404', ['error_message' => $e->getMessage()]);
         }
     }
     return response()->json($result);
 }