2017-08-11 13 views
-1

を使用することができません:私は、次のコードを使用してGoogleのサインをしようとしていたの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()); } 

関連する問題