2017-08-11 78 views
-1

我嘗試使用下面的代碼谷歌標誌:無法使用Google+ API

<code> 

function gLogin(){ //handle the login process for retrieving google User's email with 
    $client = new Google_Client(); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setScopes(array(Google_Service_Oauth2::USERINFO_EMAIL,Google_Service_Oauth2::USERINFO_PROFILE,Google_Service_Plus::PLUS_ME,Google_Service_Plus::PLUS_LOGIN)); 
    $plus = new Google_Service_Plus($client); 
    try{ 
     $client->authenticate($_GET['code']); 
     $me = $plus->people->get('me'); 
     //deal with non-domain Emails 
     if($me['domain'] !== 'xxx.org' && $me['domain'] !== 'xxx.org'){ 
      $client->revokeToken(); 
      $errorUrl = ERROR_URI . '?error=wrongDomain'; 
      http_response_code(302); 
      header('Location: ' . $errorUrl); 
      die(); 
     } 
     $displayName = $me['displayName']; 

    //get the email 
    $oauth = new Google_Service_Oauth2($client); 
    $emails = $oauth->userinfo->get(); 
    $email = $emails->getEmail(); 
    //too nested, going to sqlLogin 
    sqlLogin($email, $displayName); 
    $client->revokeToken(); 
}catch(Exception $e){ 
    error_log($e->getMessage());  
} 
} 
</code> 

當我做,我會收到以下錯誤:

{\n "error": {\n "errors": [\n {\n "domain": "usageLimits",\n
"reason": "dailyLimitExceededUnreg",\n "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",\n
"extendedHelp": " https://code.google.com/apis/console "\n }\n ],\n "code": 403,\n "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."\n }\n}\n,

任何幫助,將不勝感激

回答

0

我意識到自己的錯誤。由於我從index.php重定向,而不是保留訪問令牌,所以我不得不使用$ client-> setAccessToken($ client-> getAccessToken())手動設置它。 (由於我想要的一些代碼標準化,我沒有保持令牌)。更新的代碼:

$client = new Google_Client(); 
$client->setAuthConfig(CLIENT_SECRET_PATH); 
$client->setRedirectUri('replace_me'); //not sure if I need the redirectUri here, but it was one of the things I tested 
$client->setScopes(array(Google_Service_Oauth2::USERINFO_EMAIL,Google_Service_Oauth2::USERINFO_PROFILE,Google_Service_Plus::PLUS_ME,Google_Service_Plus::PLUS_LOGIN)); 
try{ 
    $error = $client->authenticate($_GET['code']); 
    print_r($error); 
    $client->setAccessToken($client->getAccessToken()); /*access tokens must be requested with the following line if you are not keeping them, otherwise it will be an unauthenticated request*/ 
    $plus = new Google_Service_Plus($client); 
    $me = $plus->people->get('me'); 
    //deal with non-domain Emails 
    if($me['domain'] !== 'xxx.org' && $me['domain'] !== 'xxx.org'){ 
     $client->revokeToken(); 
     $errorUrl = ERROR_URI . '?error=notDomain'; 
     http_response_code(302); 
     header('Location: ' . $errorUrl); 
     die(); 
    } 
    $displayName = $me['displayName'];  
    //get the email 
    $oauth = new Google_Service_Oauth2($client); 
    $emails = $oauth->userinfo->get(); 
    $email = $emails->getEmail(); 
    //too nested, going to sqlLogin 
    sqlLogin($email, $displayName); 
    $client->revokeToken(); 
}catch(Exception $e){ 
    error_log($e->getMessage()); }