在PHP中实施Google服务帐户示例的问题 - 获取OAUTH2错误

时间:2015-01-14 15:20:21

标签: php google-api google-api-php-client

我正在尝试使用Google服务帐户的示例。我想做的是非常基本的 - 创建连接,设置服务提供者,撤回一些数据。但是,出于某种原因,当我按照提供的示例(https://github.com/google/google-api-php-client/blob/master/examples/service-account.php)时,它会失败,并显示以下错误:

刷新OAuth2令牌时出错,消息:' {"错误" :" invalid_grant" }'

我已经搜索谷歌试图解决这个问题,并在StackOverflow上找到了一些东西,但没有一个能解决我的问题。我希望有人可以帮助我。这是我正在使用的代码:

     //initialize a new Google client
    $client = new Google_Client();
    $client->setApplicationName("books-provider-example");
    $this->service = new Google_Service_Books($client);

    //do we already have a service token?
    if (isset($this->service_token)) {
        $client->setAccessToken($this->service_token);
    }

    //setup the credentials
    $credentials = new Google_Auth_AssertionCredentials(
        $GLOBALS['GSERVICES']['clientemail'],
        $GLOBALS['GSERVICES']['scopes'],
        file_get_contents($GLOBALS['GSERVICES']['keypath']),
        'notasecret'
    );

    $client->setAssertionCredentials($credentials);

    //refresh the auth token if its expired
    if ($client->getAuth()->isAccessTokenExpired()) 
        $client->getAuth()->refreshTokenWithAssertion();

    //set the service token
    $this->service_token = $client->getAccessToken();

非常感谢任何有助于此工作的帮助!

0 个答案:

没有答案