簡體   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