经过多次研究,我现在可以使用 PHP 代码检索我的 Gmail 收件箱。现在我想知道是否可以在不强制使用 OAuth 登录的情况下获得 API 的永久 token 访问权限。
我正在制作一个小型应用程序,可以检索 3 个不同的 Gmail 收件箱,使用我的应用程序的人员不会浪费时间将我的 Gmail 应用程序授权给不同的收件箱。
我的意思是,有没有一种方法可以避免每次我尝试检索 Gmail 收件箱时都进行授权,就像我只获得一次然后将其存储到数据库中的永久 token 访问权限一样?
感谢您的帮助。
最佳答案
没有永久 token 之类的东西,但有刷新 token :
Handling authorization requests
Exchange the authorization code for an access token
The authorization code is a one-time code that your server can exchange for an access token. This access token is passed to the Gmail API to grant your application access to user data for a limited time.
If your application requires offline access, the first time your app exchanges the authorization code, it also receives a refresh token that it uses to receive a new access token after a previous token has expired. Your application stores this refresh token (generally in a database on your server) for later use.
Important: Always store user refresh tokens. If your application needs a new refresh token it must send a request with the approval_prompt query parameter set to force. This will cause the user to see a dialog to grant permission to your application again.
这是 Gmail API Quickstart 的片段
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
关于php - Gmail API : permanent token access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45834242/