繁体   English   中英

从 Google 用户信息端点获取 401

[英]Getting 401 from Google user info endpoint

我试图让用户授权应用程序进行“离线”访问。

我的令牌网址为“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...

我应该怎么做?

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 端点稳定得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM