javascript - 如何在 Firebase 函数中获取提供者访问 token ?

标签 javascript firebase firebase-authentication google-cloud-functions

我打算编写有关身份验证用户创建的 Firebase 函数。我的目标是获得 Facebook 用户页面的长期访问 token 。

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

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

我虽然 providerData 将有关于 Facebook 凭据的信息,但我只得到未定义的信息。

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

{ 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 登录,您只会在登录后获得访问 token 。 Firebase Auth 不会为您存储它(它也不存储 Facebook 刷新 token )。您需要将其保存在数据库中,只有指定的用户才能访问它,并且通过 Firebase Functions 的 Admin SDK 允许您访问它。如果您认为 Firebase Auth 应该为提供商管理 OAuth token ,请通过 Firebase 支持 channel 提交请求。 如果这对您的应用程序功能至关重要,您可以使用 Facebook API 登录用户并获取 Facebook 访问 token 并使用 firebase.auth().signInWithCredential(firebase.auth.FacebookAuthProvider.credential(fbAccessToken )) Facebook 客户端 API 可以为您管理 OAuth token 。您还可以使用后端 OAuth node.js 库在需要时通过 Firebase 函数刷新 Facebook token 。不过,您需要获取 Facebook 刷新 token 。

关于javascript - 如何在 Firebase 函数中获取提供者访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45858683/

相关文章:

javascript - 如何更改 shopify 主题名称

angular - (Angularfire) 使用 id 从 firebase 数据库中检索特定数据

android - 将多个图像上传到 firebase 存储

java - Google 登录身份验证不起作用

node.js - Firebase Cloud Firestore 触发器 context.auth 始终为 null

javascript - firebase 电子邮件验证网络

react-router - 为 firebase google auth 指定重定向 url

javascript - 如何在dom中存储和追加大字符串

JavaScript 第二个循环不会改变 CSS

javascript - 在 React 中使用内联样式对象与使用普通 css 类