使用firebase auth令牌使用JS SDK进行图形api调用

时间:2016-06-05 02:51:14

标签: javascript facebook facebook-graph-api firebase facebook-javascript-sdk

我目前有一个用应用程序编写的Facebook应用程序,它使用firebase进行身份验证并登录我的应用程序。我正在使用firebase auth获取访问令牌。我想使用此令牌来进行图形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."

2 个答案:

答案 0 :(得分:3)

你可以做@ muschn说的话,或者你也可以用facebook api做一个简单的http请求。

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

您可以从facebook的图形API获取字段,访问令牌是您从firebase获得的字段。

答案 1 :(得分:2)

使用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