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

How to use Firebase authentication token to list user blogs using js

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

I'm trying to retrieve authenticated user blogs (granted scope):

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) );

but i get error:

{
  "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"
  }
}

Could you tell me please what I'm missing to achieve this without using back end ?

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

A Firebase Authentication ID token is a JWT. Blogger expects an OAuth 2 token, which the Firebase Authentication token isn't.

While it is possible within Firebase to create an ID Token based on an OAuth token, the reverse isn't possible. You will have to sign in the user with an OAuth 2 provider instead, and pass that token to blogger.