重新使用 Google Api Bearer Token 访问用户的云端硬盘
Reusing Google Api Bearer Token to access user's Drive
我有以下内容:
gapi.auth.authorize(
{ client_id: CLIENT_ID, scope: SCOPES, immediate: false },
handleAuthResult);
这让我可以访问 access_token
:
目标是此应用程序的用户可以授予对我的应用程序的访问权限,以使用他们的 google 驱动器来存储他们的内容。我需要能够使用此令牌来:
- 读取文件
- Post 个文件
长时间未通过 google 登录页面重新验证。如何实现?我正在使用 C# 作为后端。
您需要从在浏览器中 JavaScript 中执行 OAuth 切换到在您的服务器上执行 OAuth。此处有详细记录 https://developers.google.com/identity/protocols/OAuth2WebServer
您可以选择使用 OAuth c# 库,或直接调用 REST 端点。
要在用户不在场的情况下实现访问,您需要access_type=offline
...
Set the value to offline if your application needs to refresh access
tokens when the user is not present at the browser. This is the method
of refreshing access tokens described later in this document. This
value instructs the Google authorization server to return a refresh
token and an access token the first time that your application
exchanges an authorization code for tokens.
您将在用户第一次授予权限时获得刷新令牌。您需要将此刷新令牌保存在某种服务器数据存储中。然后您可以随时使用它来请求访问令牌。
我有以下内容:
gapi.auth.authorize(
{ client_id: CLIENT_ID, scope: SCOPES, immediate: false },
handleAuthResult);
这让我可以访问 access_token
:
目标是此应用程序的用户可以授予对我的应用程序的访问权限,以使用他们的 google 驱动器来存储他们的内容。我需要能够使用此令牌来:
- 读取文件
- Post 个文件
长时间未通过 google 登录页面重新验证。如何实现?我正在使用 C# 作为后端。
您需要从在浏览器中 JavaScript 中执行 OAuth 切换到在您的服务器上执行 OAuth。此处有详细记录 https://developers.google.com/identity/protocols/OAuth2WebServer 您可以选择使用 OAuth c# 库,或直接调用 REST 端点。
要在用户不在场的情况下实现访问,您需要access_type=offline
...
Set the value to offline if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.
您将在用户第一次授予权限时获得刷新令牌。您需要将此刷新令牌保存在某种服务器数据存储中。然后您可以随时使用它来请求访问令牌。