javascript - 在 Google Contacts API 版本 3.0 上使用 oauth2 检索刷新 token

标签 javascript node.js google-api oauth-2.0 google-contacts-api

我正在使用 oauth2(node.js 和 connect-oauth 库)连接到 google contacts API version 3.0 .

这样做,我会得到如下响应:

{ access_token : "...",
"token_typen": "Bearer",
"expires_in" : 3600,
"id_token": "..." }

我缺少用于在新访问 token 过期后立即获取新访问 token 的刷新 token 。

oauth2 的选项

{ host: 'accounts.google.com',
  port: 443,
  path: '/o/oauth2/token',
  method: 'POST',
  headers: 
   { 'Content-Type': 'application/x-www-form-urlencoded',
     Host: 'accounts.google.com',
     'Content-Length': 247 } }

后正文 'redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback&grant_type=authorization_code&client_id=CLIENTID&client_secret=CLIENTSECRET&type=web_server&code=4%2F3gbiESZTEOjiyFPLUhKfE_a_jr8Q'

注意:我试图从 similar question 添加 approval_prompt=force到 request-post_body 但这导致了错误

{ statusCode: 400, data: '{\n  "error" : "invalid_request"\n}' }

最佳答案

NOTE: I tried to add approval_prompt=force from a similar question to the request-post_body but this resulted in an Error

请求 token 时不需要approval_prompt 参数。 *approval_prompt* 参数用于授权部分。


I am missing the refresh token...

您不会获得 *refresh_token* 的唯一方法是:

因此,尝试在授权码请求中添加:access_type=offline

编辑:

即:

https://accounts.google.com/o/oauth2/auth?client_id=**your_client_id**&scope=https://www.googleapis.com/auth/plus.me&redirect_uri=http://localhost&response_type=code&access_type=offline

如果您得到 400 是因为您添加了无效参数或缺少参数。

祝你好运!

关于javascript - 在 Google Contacts API 版本 3.0 上使用 oauth2 检索刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087429/

相关文章:

javascript - 像普通用户一样使用你的机器人

C# 为 AuthorizeAsync 设置 access_type = offline?

javascript - 检查两个整数是否具有相同的符号

javascript - 文件上传控件响应式(Reactive)表单

node.js - 我收到错误,无法使用 mocha 找到模块 './--require.js'

google-analytics - Google Discovery API 请求缺少有效的 API key

google-api - 如何授权服务帐户访问 Google Admin API

javascript - 麻烦切换类(jquery)

javascript - 单选按钮+复选框组合

javascript - Node.JS 没有附加字符串奇怪的错误?