Chrome 身份 API 中的 OAuth2 用户信息

OAuth2 User Info in Chrome Identity API

我正在使用 Google Chrome 身份 API,我成功获得了访问令牌并将其发送到我的后端服务器。

为了验证我使用的访问令牌 URL :

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=xxxxxx

我需要获取已验证用户的 userId,我该怎么做?

我在 manifest.json 文件中使用这个范围。

https://www.googleapis.com/auth/plus.profile

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=xxxxxx 的响应包括 userId 作为 sub 值。

{
 "azp": "407408718192.apps.googleusercontent.com",
 "aud": "407408718192.apps.googleusercontent.com",
 "sub": "114233674199568482864",
 "scope": "https://www.googleapis.com/auth/userinfo.profile",
 "exp": "1436369027",
 "expires_in": "3589",
 "access_type": "offline"
}

或者您可以向 people.get 发出请求并使用 id 值。

{
 "kind": "plus#person",
 "etag": "\"RqKWnRU4WW46-6W3rWhLR9iFZQM/gLREuMULAxw3wiBcudkUCAWTcjE\"",
 "objectType": "person",
 "id": "114233674199568482864",
 "displayName": "Abraham Williams",
 ...
}

我的解决方案:

当我在 interactive mode 中请求访问令牌时,我得到了 userIdSub 参数),但是如果我在 interactive = false 模式下执行响应没有 sub 参数。

chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
  // code
}