Gmail API与PHP的使用

huangapple go评论85阅读模式
英文:

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.0PHP 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.

huangapple
  • 本文由 发表于 2023年8月9日 11:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定