duande9301 2015-01-14 15:20
浏览 124

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

I am trying to implement an example of using a google services account. What I want to do is very basic - create a connection, set the service provider, pull back some data. For some reason however, when I follow the example provided (https://github.com/google/google-api-php-client/blob/master/examples/service-account.php) it fails with the following error:

Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'

I have scoured google trying to figure this out, and have found a few things here on StackOverflow, but none of them have solved my issue. Im hoping someone out there can help me. Here is the code I am working with:

     //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();

Any help in getting this to work is greatly appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
    • ¥15 cs loadimage运行不了,easyx也下了,没有用
    • ¥15 r包runway详细安装教程
    • ¥15 Html中读取Json文件中数据并制作表格
    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥20 关于变压器的具体案例分析