Search code examples
oauth-2.0firebase-authenticationfetchgoogle-oauthgoogle-blogger-api

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


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 ?


Solution

  • 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.