doushang1964 2016-03-03 10:11
浏览 272

Google_Client刷新令牌在几天后过期

I have a problem with the refresh tokens google returns. I think they expire after some days. Documentation says that they do not unless the user does this manually.

Here is what I do:

I prompt the user to authorize my app using Google_Client

$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_REDIRECT_URI);
$client->setScopes(array(GOOGLE_SCOPE_A,'https://www.googleapis.com/auth/userinfo.email'));
$client->setApplicationName(GOOGLE_APPLICATION_NAME);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');

I store the refresh token in the database using

$refreshToken = $client->getRefreshToken();

I have a cURL that runs every day and pulls some data from analytics, it then connects using the following code:

$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->refreshToken($refreshToken);
$client->isAccessTokenExpired() //this returns false;

Everything works fine for the first 15-30 days. After that, it looks like the refresh token is not working any more.

$client->isAccessTokenExpired() //this returns true;

and when I am trying to pull data from analaytics I am getting a Google_Service_Exception with code:401 and the message:"Login Required"

What I do then is prompt the user to authorize my app again, update the refresh token with the new one in the database and everything is working as expected for the next 15-30 days until the above problem happens again.

Does anyone have any idea why this is happening and how can I prevent this?

Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起