2013-03-20 110 views
-1

我已經在開發服務器上實現了google plus api,它工作正常。我在生產服務器上使用了相同的代碼。但是在請求權限之後,需要很長時間才能返回到我的網站並登錄。Google Plus登錄API無法在生產服務器上工作

任何人都可以請讓我知道可能是什麼原因。我用過oauth2。

下面是我使用

<?php 
session_start(); 
require_once 'googleplus/src/Google_Client.php'; 
require_once 'googleplus/src/contrib/Google_Oauth2Service.php'; 

class Webpage_UserGPlusLogin extends Webpage 
{ 
    public function __construct() 
    { 
     $temp_redirect = $_SESSION['RETURN_URL_AFTERLOGIN']; 
     $this->title = 'User Account'; 

     $client = new Google_Client(); 
     $client->setApplicationName(WEBSITE_NAME); 
     $client->setClientId(GOOGLE_PLUS_CLIENT_ID); // Client Id 
     $client->setClientSecret(GOOGLE_PLUS_CLIENT_SECRET); // Client Secret 
     $client->setRedirectUri(GOOGLE_PLUS_REDIRECT_URI); // Redirect Uri set while creating API account 
     $client->setDeveloperKey(GOOGLE_PLUS_DEVELOPER_KEY); // Developer Key 
     $oauth2 = new Google_Oauth2Service($client); 


     if (isset($_GET['code'])) { 
      $client->authenticate($_GET['code']); 
      $_SESSION['token'] = $client->getAccessToken(); 
      $redirect = GOOGLE_PLUS_REDIRECT_URI; 
      header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); // Redirects to same page 
      return; 
     } 

     if (isset($_SESSION['token'])) { 
      $client->setAccessToken($_SESSION['token']); 
     } 

     if (isset($_REQUEST['logout'])) { 
      unset($_SESSION['token']); 
      $client->revokeToken(); 
     } 

     if(!isset($_SESSION['email_address_user_account'])) // Check if user is already logged in or not 
     { 
      if ($client->getAccessToken()) { 
       $user = $oauth2->userinfo->get(); // Google API call to get current user information 

       $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); 
       $img = filter_var($user['picture'], FILTER_VALIDATE_URL); 
       $googleuserid = $user['id']; 
       $given_name = $user['given_name']; 
       $family_name = $user['family_name']; 
       // The access token may have been updated lazily. 
       $_SESSION['token'] = $client->getAccessToken(); 

       // If email address is present in DB return user data else insert user info in DB 
       $this->result = UserAccount::gplus_sign_up($email, $googleuserid, $given_name, $family_name); 

       // Create new user object. 
       $this->user_account = new UserAccount($this->result['id'],$this->result['email_address'],$this->result['password'],$this->result['confirmation_code'],$this->result['is_confirmed'], $this->result['first_name'], $this->result['last_name']); 

       $_SESSION['gplus_email_address'] = $email; 
       $_SESSION['gplus_first_name'] = $given_name; 
       $_SESSION['gplus_last_name'] = $family_name; 
       $_SESSION['gplus_id'] = $googleuserid; 
       $_SESSION['gplus_profile_pic'] = $img; 
       $_SESSION['email_address_user_account'] = $email; 

      } else { 
       $authUrl = $client->createAuthUrl(); 
      } 
     } 

     if(isset($temp_redirect)) 
      header("Location:".$temp_redirect); 
     else 
      header("Location:/"); 
    } 
} 

在此先感謝

+0

當你嘗試使用生產站點進行連接時,瀏覽器的JavaScript控制檯中是否有任何東西出現? 您還指出您使用的是相同的代碼,您是否也在API控制檯中使用相同的註冊? 最後,你說它需要很長時間才能返回 - 在此之後剩下的認證流程是否真的有效?或者你看到其他問題? – Prisoner 2013-03-20 14:32:04

+0

這是非常含糊的,你可以發佈你的代碼,你正在啓動流程,以幫助我們診斷問題?此外,如果您能夠發佈鏈接到該網站的問題正在發生,可能也有幫助。 – BrettJ 2013-03-20 15:21:35

+0

@Prisoner我試過使用相同的API控制檯以及不同的,但沒有成功:(,是的認證流程工作,但它需要很長時間說2-3分鐘。 – user2147879 2013-03-21 07:37:37

回答

1

試試這個

使用下面的代碼

instade的

$temp = json_decode($_SESSION['token']); 
$request = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";     
$curl = curl_init(); 
curl_setopt($curl,CURLOPT_URL,$request); 
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); 
curl_setopt($curl,CURLOPT_TIMEOUT,30); 
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($curl, CURLINFO_HEADER_OUT, true); 
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization: OAuth '.$temp->access_token)); 
$response = trim(curl_exec($curl)); 
$info = curl_getinfo($curl); 
$request_header_info = curl_getinfo($curl, CURLINFO_HEADER_OUT); 

//var_dump($info); 
//var_dump($request_header_info);   
curl_close($curl); 
echo "<pre>"; 
print_r(json_decode($response));  

代碼10

希望這會對你有所幫助.. :)

相關問題