无法使用Google+ API
问题描述:
我尝试使用下面的代码谷歌标志:无法使用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,
任何帮助,将不胜感激
答
我意识到自己的错误。由于我从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()); }