如何在 Firebase 函数中获取提供者访问令牌? [英] How to get provider access token in Firebase functions?

查看:32
本文介绍了如何在 Firebase 函数中获取提供者访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在创建身份验证用户时编写 Firebase 函数.我的目标是获取 Facebook 用户页面的长期访问令牌.

I'm planning to write Firebase function on Authentication user creation. My goal is to get the long live access token for the Facebook user page's.

为此,我需要 Firebase 函数中的用户访问令牌.在下面尝试过..,

To do that I need user access token in Firebase functions. Tried below..,

exports.saveLongLiveToken = functions.auth.user().onCreate(event => {
  console.log(event.providerData)
})

我虽然 providerData 将拥有有关 Facebook 凭据的信息,但不是我只得到了未定义的信息.

I though providerData will have the information about the Facebook credentials, but not instead I got only undefined.

注意:在事件中,我得到了下面

Note: In the event, I got below

{ data: 
   { displayName: 'Rob',
     email: 'xxx@xyz.com',
     metadata: 
      { createdAt: 2017-08-23T18:58:34.000Z,
        lastSignedInAt: 2017-08-23T18:58:34.000Z },
     photoURL: 'http...',
     providerData: [ [Object] ],
     uid: 'xfff...' },
  eventId: 'xxx',
  eventType: 'providers/firebase.auth/eventTypes/user.create',
  notSupported: {},
  resource: 'projects/fiobot-4fa94',
  timestamp: '2017-08-23T18:58:34.571Z',
  params: {} }

推荐答案

如果您是使用 Firebase Auth 的 signInWithPopup/Redirect 登录,您将只能在登录后获得访问令牌.Firebase Auth 不会为您存储它(它也不会存储 Facebook 刷新令牌).您需要将其保存在数据库中,只有指定的用户可以访问它,并且通过 Firebase 函数的 Admin SDK 可以允许您访问它.如果您认为 Firebase Auth 应该为提供商管理 OAuth 令牌,请通过 Firebase 支持渠道提出请求.如果这对您的应用功能至关重要,您可以使用 Facebook API 登录用户并获取 Facebook 访问令牌并使用 firebase.auth().signInWithCredential(firebase.auth.FacebookAuthProvider.credential(fbAccessToken) 登录))Facebook 客户端 API 可以为您管理 OAuth 令牌.您还可以在需要时使用后端 OAuth node.js 库通过 Firebase 函数刷新 Facebook 令牌.不过,您需要获取 Facebook 刷新令牌.

If you are signing in with Firebase Auth signInWithPopup/Redirect, you will only get the access token after sign-in. Firebase Auth does not store it for you (neither does it store the Facebook refresh token). You will need to save it in the Database where only the specified user can access it and the Admin SDK via Firebase Functions can allow you access it. If you think Firebase Auth should manage OAuth tokens for providers, please file a request via Firebase Support channels. If this is essential for your app functionality, you can use the Facebook API to sign in the user and get a Facebook access token and sign in with firebase.auth().signInWithCredential(firebase.auth.FacebookAuthProvider.credential(fbAccessToken)) Facebook client API can manage the OAuth token for you. You can also use a backend OAuth node.js library to refresh Facebook tokens via the Firebase Functions whenever you need one. You would need to get the Facebook refresh token though.

这篇关于如何在 Firebase 函数中获取提供者访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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