2017-07-03 73 views
0

這是我用於Google登錄的控制器。代碼在谷歌響應代碼後不起作用。谷歌登錄codeigniter谷歌中的回執代碼中帶「/」的值代碼打破代碼

public function index(){ 
    // Include the google api php libraries 
    require_once APPPATH.'libraries/google-api-php-client/vendor/autoload.php'; 

    // Google Project API Credentials 
    $clientId = 'mentioned'; 
    $clientSecret = 'mentioned'; 
    $redirectUrl = base_url() . 'user_authentication/'; 


    // Google Client Configuration 
     $gClient = new Google_Client(); 
     $gClient->addScope('email'); 
     $gClient->setApplicationName('REDBUD HOTELS'); 
     $gClient->setClientId($clientId); 
     $gClient->setClientSecret($clientSecret); 
     $gClient->setRedirectUri($redirectUrl); 
     $google_oauthV2 = new Google_Service($gClient); 
//GOOGLE response 
     if (isset($_REQUEST['code'])) 
        { 
        $gClient->authenticate($_REQUEST['code']); 
        $this->session->set_userdata('token', $gClient->getAccessToken()); 
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);  

        } 
//check if $token us set 
     $token = $this->session->userdata('token'); 
     if (!empty($token)) 
      { 
      $gClient->setAccessToken($token); 
      } 

     if ($gClient->getAccessToken()) 
      { 
      $userProfile = $plus->userinfo->get(); 
      // Preparing data for database insertion 
      $userData['oauth_provider'] = 'google'; 
      $userData['oauth_uid'] = $userProfile['id']; 
      $userData['first_name'] = $userProfile['given_name']; 
      $userData['last_name'] = $userProfile['family_name']; 
      $userData['email'] = $userProfile['email']; 
      $userData['gender'] = $userProfile['gender']; 
      $userData['locale'] = $userProfile['locale']; 
      $userData['profile_url'] = $userProfile['link']; 
      $userData['picture_url'] = $userProfile['picture']; 
      // Insert or update user data 
      $userID = $this->user->checkUser($userData); 
      if(!empty($userID)) 
      { 
       $data['userData'] = $userData; 
       $this->session->set_userdata('userData',$userData); 
      } else 
      { 
       $data['userData'] = array(); 
      } 
     } else 
     { 
      $data['authUrl'] = $gClient->createAuthUrl(); 
     } 
     $this->load->view('user_authentication/index',$data); 
    } 

「響應代碼從谷歌user_authentication /?代碼= 4/5wcPW5Yv6qNONOzeUz5gun4PtfuPTa6mPN9r97BDhGM#」 也請您告訴我應該是什麼更好的$plus->userinfo->get() or $google_oauthV2->userinfo->get();

回答

0

谷歌響應代碼確實有 「/」

ex:http://www.example.com/storage_integration/granted?code=4/oXKtlTyfmtzUBc5CjbafSpwItMz7itZMhM54VpKEbwI

您可以使用$_GET代替$_REQUEST

if (isset($_GET['code'])) { 
    // authenticate auth code here 
} 
+0

我試過這個,但由於codeigniter用戶友好的網址這個谷歌響應給出了一個錯誤 –

+0

@Pawansharma你可以發佈完整的錯誤信息? –

+0

我對這個問題發生了過度反應,它只是通過替換對象new Google_Service($ gClient)來實現google api更新;使用Google_Service_Oauth2($ gClient);解決了我的問題。 –

0

通過更換隻是反對新GOOGLE_SERVICE($ gClient);使用Google_Service_Oauth2($ gClient);解決了錯誤