【问题标题】:Getting 401 from Google user info endpoint从 Google 用户信息端点获取 401
【发布时间】:2021-07-29 04:47:04
【问题描述】:

我正在尝试让用户授权该应用进行“离线”访问。

我的令牌网址为“https://oauth2.googleapis.com/token”。我使用诸如一次性codeclient_idclient_secretscopes 等参数来获得包含令牌的响应。如下所示:

{
  access_token: ...,
  expires_in: 3599,
  refresh_token: ...,
  scope: ...,
  token_type: 'Bearer'
}

我从上述响应中获取 access_token 值并尝试使用它从“https://www.googleapis.com/oauth2/v1/userinfo?alt=json”中提取userInfo,它总是返回 401 .

我是用代码和 curl 完成的,试图将授权标头中的令牌传递为

const headers = {
  Authorization: `Bearer ${accessToken}`
};
const userInfo = await fetch('https://www.googleapis.com/oauth2/v1/userinfo?alt=json',{headers});

并且还尝试将 access_token 添加到查询字符串中,并针对https://www.googleapis.com/oauth2/v2/userinfo?alt=jsonhttps://www.googleapis.com/oauth2/v4/userinfo?alt=json 等较新的端点执行。

我总是收到 401。消息是:Request is missing required authentication credentials...

我应该怎么做?

【问题讨论】:

  • 如果你有访问令牌的值,你能用一个简单的 cURL 命令重现这个问题吗: curl -H "Authorization: Bearer youraccesstokenhere" "googleapis.com/oauth2/v2/userinfo?alt=json"
  • 我不想重复这个问题。我需要知道为什么它不能按我想要的那样工作,所以我可以修复它。

标签: javascript oauth-2.0 jwt google-oauth userinfo


【解决方案1】:

userinfo 端点是一个 HTTP GET 调用,您可以将访问令牌字符串作为查询参数。

https://openidconnect.googleapis.com/v1/userinfo?access_token=ya29.a0AfH6SMDfRQFdEOxq97LXqUa1jwdtRLSD2l_hiLFeaWYXRBB6gIkh7XHko75xj70uPnuOppKtf0c 

回应

{
  "sub": "1172004755326746",
  "name": "Linda Lawton",
  "given_name": "Linda",
  "family_name": "Lawton",
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GhroCYJp2P9xeYeYk1npchBPK-zbtTxzNQo0WAHI20\u003ds96-c",
  "locale": "en"
}

顺便说一句,您可能需要考虑使用people api,而不是它比 userinfo 端点更稳定。

【讨论】:

    猜你喜欢
    • 2012-11-28
    • 2016-04-12
    • 2018-07-21
    • 2019-08-11
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多