php - Gmail API : permanent token access

标签 php access-token gmail-api

经过多次研究,我现在可以使用 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/

相关文章:

javascript - 是否可以添加一个带有端口的pushEndpoint?

java - 当我尝试通过我的 Google Suite 帐户发送电子邮件时,Gmail API 返回 failedPrecondition 错误

php - 可以用 php 将我的文本表单更新为 mysql

php - file_get_contents 无限客户端页面刷新

Facebook 图表 API : simple search for users by name

jwt - 使用 OpenId (Cognito) 进行身份验证后,如何在 Blazor WebAssembly 中获取 id_token?

linux - Nginx 在 PHP 解析错误后发送状态 200

javascript - 如何在 isset 中取消设置 php 中的全局变量

java - 如何在android App中实现基于Token的Rest API

imap - 是什么让 Gmail API 比 IMAP 更高效?