Warm tip: This article is reproduced from serverfault.com, please click

oauth 2.0-如何使用Firebase身份验证令牌使用js列出用户博客

(oauth 2.0 - How to use Firebase authentication token to list user blogs using js)

发布于 2020-11-26 02:39:17

我正在尝试检索经过身份验证的用户博客(授权范围):

var token = await firebase.auth().currentUser.getIdToken();

fetch('https://www.googleapis.com/blogger/v3/users/self/blogs', {
  "headers": {
    "Authorization": "Bearer " + token
  },
  "method" : "GET",
  "muteHttpExceptions": true
}).then( r => console.log(r) );

但我得到错误:

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

你能告诉我不使用后端实现此目标所缺少的吗?

Questioner
Muhammad Saleh
Viewed
11
Frank van Puffelen 2020-11-26 10:56:02

Firebase身份验证ID令牌是JWT。Blogger需要OAuth 2令牌,而Firebase身份验证令牌则不需要。

尽管可以在Firebase中基于OAuth令牌创建ID令牌,但相反的操作是不可能的。你必须改为使用OAuth 2提供程序登录用户,然后将该令牌传递给博客。