英文:
Gmail API with PHP
问题
我正在使用Gmail API来读取邮件并添加/删除标签。
我已经正确进行了身份验证,获得了有效期为3600秒的访问令牌。
问题开始出现在这里...当我尝试刷新令牌时,我得到了一个空值,所以我无法刷新它。
一些代码:
$this->client = new Client();
$this->client->setApplicationName('App');
$this->client->setClientId('xxxxxxx');
$this->client->setClientSecret('xxxxxxx');
$this->client->setRedirectUri('xxxxxxx/oauth/gmail/callback');
$this->client->setScopes([GoogleGmail::GMAIL_READONLY, GoogleGmail::GMAIL_MODIFY]);
$this->client->setPrompt('select_account consent');
$this->client->setAccessType('offline');
在回调函数中,我执行以下操作:
$this->client->fetchAccessTokenWithAuthCode($code);
输出结果:
{
"access_token":"xxxxxxxxxx",
"expires_in":3599,
"scope":"https:\/\/www.googleapis.com\/auth\/gmail.readonly https:\/\/www.googleapis.com\/auth\/gmail.modify",
"token_type":"Bearer",
"created":7374384324
}
我保存了访问令牌、过期时间和所有的数据。但是我没有得到刷新令牌。
我尝试了以下代码:
$this->client->getRefreshToken(); // null
如果我执行以下代码:
$this->client->isAccessTokenExpired()
无论我是在5秒钟前进行的身份验证,我始终得到true
。
我正在使用google/apiclient v2.15.0
和PHP 8.1
。
谢谢。
英文:
I'm using the Gmail API for reads emails and just add/remove labels.
I authenticated correctly, I get my access token with 3600 seconds of life.
And here starts the problems... When I try to Refresh Token, I got a null, so I can't refresh it.
Some code:
$this->client = new Client();
$this->client->setApplicationName('App');
$this->client->setClientId('xxxxxxx');
$this->client->setClientSecret('xxxxxxx');
$this->client->setRedirectUri('xxxxxxx/oauth/gmail/callback');
$this->client->setScopes([GoogleGmail::GMAIL_READONLY, GoogleGmail::GMAIL_MODIFY]);
$this->client->setPrompt('select_account consent');
$this->client->setAccessType('offline');
In the callback I do:
$this->client->fetchAccessTokenWithAuthCode($code);
Output:
{
"access_token":"xxxxxxxxxx",
"expires_in":3599,
"scope":"https:\/\/www.googleapis.com\/auth\/gmail.readonly https:\/\/www.googleapis.com\/auth\/gmail.modify",
"token_type":"Bearer",
"created":7374384324
}
I saved the Access Token, Expired time, and all data really. But I don't get the refresh token.
I try with:
$this->client->getRefreshToken(); // null
And If I do
$this->client->isAccessTokenExpired()
I always get true
, inclusive if I auth 5 seconds ago.
I'm using google/apiclient v2.15.0
, PHP 8.1
Thanks
答案1
得分: 1
你只能在用户首次验证你的应用程序时获得刷新令牌。你需要将其与用户的姓名一起存储在你的应用程序中,这样当你想要请求一个新的访问令牌时,你就可以发送它。
在Web应用程序中刷新访问令牌不会返回一个新的刷新令牌。再次请求授权也不会导致获得新的刷新令牌。
英文:
You only get a refresh token with the first time the user has authenticated your application. You need to store that in your application along with the users name so that you can then send that when you would like to request a new access token.
A refresh of an access token in a web app will not return a new refresh token. Requesting authorization again will also not result in a new refresh token.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论