使用 firebase 身份验证令牌使用 JS SDK 进行图形 api 调用

Using firebase auth token to make graph api calls using JS SDK

我目前有一个使用 firebase 验证和登录我的应用程序的应用程序编写的 Facebook 应用程序。我正在使用 firebase 身份验证获取访问令牌。我想使用此令牌来进行图形 api 调用,例如

FB.api(
     '/me/albums',
     'GET',
     {},
     function(response) {
        // Insert your code here
        console.log(response);
        console.log(token);
     }
  );

我正在按照documentation on firebase进行身份验证。用户已在 firebase 上成功创建,用户已授予照片访问权限。我只是无法弄清楚如何使用令牌来调用 Facebook 的图表 api。

 var provider = new firebase.auth.FacebookAuthProvider();
 provider.addScope('user_photos');
 provider.addScope('user_friends');
 firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Facebook Access Token. You can use it to access the Facebook API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
});

编辑 当我在重定向后拨打电话时,出现以下错误

"An active access token must be used to query information about the current user."

在使用 JS SDK 时,您无需在授权后处理用户令牌。我不确定它如何与 Firebase 一起使用,但我假设如果你想在使用 Firebase 登录后使用 JS SDK,你必须自己添加令牌:

FB.api(
    '/me/albums', {fields: '...', access_token: token}, function(response) {
        console.log(response);
    }
);

此外,请确保访问令牌有效并包含 user_photos 权限。你可以在这里调试它:https://developers.facebook.com/tools/debug/accesstoken/

您也可以尝试使用 Fetch API 而不是 JS SDK 来使用来自 Firebase 的令牌进行 API 调用:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

您可以按照@luschn 所说的进行操作,或者您也可以使用 facebook api 发出一个简单的 http 请求。

$.get(
     "https://graph.facebook.com/me",
     {
        'fields'       : fields,
        'access_token' : token
     },
     function(response) {
     //enter code here
     }
)

您可以从 facebook 的图表中获取字段 api 并且访问令牌是您从 firebase 中获取的。