如何使用Firebase身份验证令牌使用js列出用户博客 [英] How to use Firebase authentication token to list user blogs using js

查看:62
本文介绍了如何使用Firebase身份验证令牌使用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) );

但是我得到了错误:

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

推荐答案

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

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

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

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.

这篇关于如何使用Firebase身份验证令牌使用js列出用户博客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆