Google exception error refreshing token error : invalid_grant
Google exception error refreshing token error : invalid_grant
我正在尝试使用服务帐户访问 google 网站管理员 api。但我无法
PHP 致命错误:未捕获异常 'Google_Auth_Exception' 消息 'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}''
我尝试了其他帖子中提到的解决方案,
1.安装ntp
2. date_default_timezone_set(“Asia/Kolkata”);
3. sudo ntpdate pool.ntp.org
但是 none 奏效了。请有人帮忙。我卡住了。
require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/src/Google/autoload.php');
session_start();
$client_email = '123456789-abcdebsbjf@developer.gserviceaccount.com';
$private_key = file_get_contents('Config/myproject.p12');
$scopes = array('https://www.googleapis.com/auth/webmasters');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Webmasters($client);
$list = $service->sitemaps->listSitemaps("www.example.com");
print_r($list);
经过一番努力,我自己解决了问题。遵循 link 我提到相对路径的代码是一个愚蠢的小错误:
$private_key = file_get_contents('Config/myproject.p12');
当我用绝对路径替换它时它起作用了,即:
$private_key = file_get_contents(dirname(__FILE__) . '/Config/myproject.p12');
我正在尝试使用服务帐户访问 google 网站管理员 api。但我无法 PHP 致命错误:未捕获异常 'Google_Auth_Exception' 消息 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }''
我尝试了其他帖子中提到的解决方案, 1.安装ntp 2. date_default_timezone_set(“Asia/Kolkata”); 3. sudo ntpdate pool.ntp.org
但是 none 奏效了。请有人帮忙。我卡住了。
require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/src/Google/autoload.php');
session_start();
$client_email = '123456789-abcdebsbjf@developer.gserviceaccount.com';
$private_key = file_get_contents('Config/myproject.p12');
$scopes = array('https://www.googleapis.com/auth/webmasters');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Webmasters($client);
$list = $service->sitemaps->listSitemaps("www.example.com");
print_r($list);
经过一番努力,我自己解决了问题。遵循 link 我提到相对路径的代码是一个愚蠢的小错误:
$private_key = file_get_contents('Config/myproject.p12');
当我用绝对路径替换它时它起作用了,即:
$private_key = file_get_contents(dirname(__FILE__) . '/Config/myproject.p12');