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

查看:107
本文介绍了如何在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 Functions的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天全站免登陆