繁体   English   中英

使用Firebase身份验证令牌使用JS SDK进行图形API调用

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

我目前有一个使用app编写的Facebook应用,该应用使用firebase进行身份验证并登录到我的应用。 我正在使用Firebase身份验证获取访问令牌。 我想使用此令牌进行图形API调用,例如

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

我正在按照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."

您可以执行@luschn所说的,也可以使用facebook api发出简单的http请求。

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

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

使用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使用Token进行API调用: https : //developer.mozilla.org/en-US/docs/Web/API/Fetch_API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM