【问题标题】:Google exception error refreshing token error : invalid_grant谷歌异常错误刷新令牌错误:invalid_grant
【发布时间】: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


    【解决方案1】:

    经过一番挣扎,我自己解决了问题。在我提到相对路径的代码链接中,这是一个愚蠢的小错误:

    $private_key = file_get_contents('Config/myproject.p12');
    

    当我用绝对路径替换它时它起作用了,即:

    $private_key = file_get_contents(dirname(__FILE__) . '/Config/myproject.p12'); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 2021-09-11
      相关资源
      最近更新 更多