简体   繁体   English

从 Google 用户信息端点获取 401

[英]Getting 401 from Google user info endpoint

I am trying to get users to authorize the app for "offline" access.我试图让用户授权应用程序进行“离线”访问。

I have the token url as "https://oauth2.googleapis.com/token".我的令牌网址为“https://oauth2.googleapis.com/token”。 I hit that with params such as the one-time code , client_id , client_secret , scopes etc to get a response containing tokens.我使用一次性codeclient_idclient_secretscopes等参数来获取包含令牌的响应。 It looks like the below:它看起来像下面这样:

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

I take the access_token value from the above response and try to use it to pull userInfo from "https://www.googleapis.com/oauth2/v1/userinfo?alt=json" and it is always returning 401.我从上面的响应中获取 access_token 值并尝试使用它从“https://www.googleapis.com/oauth2/v1/userinfo?alt=json”中提取userInfo并且它总是返回 401。

I did it with code and curl, trying to pass the token in the authorization header as我用代码和 curl 做到了,试图将授权标头中的令牌传递为

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

and also tried to add the access_token to the query string, as well as executing against newer endpoints such as https://www.googleapis.com/oauth2/v2/userinfo?alt=json and https://www.googleapis.com/oauth2/v4/userinfo?alt=json .并尝试将 access_token 添加到查询字符串,以及针对较新的端点执行,例如https://www.googleapis.com/oauth2/v2/userinfo?alt=jsonhttps://www.googleapis.com/oauth2/v4/userinfo?alt=json

I am always getting 401. The message is: Request is missing required authentication credentials...我总是收到 401。消息是: Request is missing required authentication credentials...

What should I be doing instead?我应该怎么做?

The userinfo end point is a HTTP GET call you can string the access token as a query parameter. userinfo 端点是一个 HTTP GET 调用,您可以将访问令牌字符串作为查询参数。

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

Response回复

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

BTW you may want to consider using the people api in stead its much more stable then the userinfo endpoint.顺便说一句,您可能需要考虑使用people api来代替它比 userinfo 端点稳定得多。

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

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