【发布时间】:2016-01-07 02:37:38
【问题描述】:
我正在尝试使用服务帐户访问 google webmaster api。但我无法 PHP 致命错误:未捕获的异常 'Google_Auth_Exception' 带有消息'刷新 OAuth2 令牌时出错,消息:'{ “错误”:“invalid_grant” }''
我尝试了其他帖子中提到的以下解决方案, 1.安装ntp 2. date_default_timezone_set(“亚洲/加尔各答”); 3. sudo ntpdate pool.ntp.org
但没有一个有效。请有人帮忙。我被卡住了。
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);
【问题讨论】:
标签: php api exception oauth-2.0 google-search-console