6

I'm just a little confused as to how to pass my access token into FB.api() when making requests for protected things.

I'm getting my app to login and authenticate fine, but how do I use FB.api() with the access token I have?

app.accessToken = response.authResponse.accessToken; // This is a valid access token.

FB.api('/me/friends?access_token='+app.accessToken, {fields: 'name,id,location,picture,installed'}, function(response) {
    console.log(response);
});

Is that the correct way to pass in the access token to FB.api()?

In this case, my response comes back with the friends name,id,location,picture but it doesn't seem to have the 'installed' data as that is protected.

Am I doing this right?

4

4 回答 4

9

尽管我明白为什么有些用户会说由于您的特定用途,您可能不需要传递访问令牌。

通常,在某些情况下,您确实需要通过 FB.api() 传递访问令牌

这样做的方法是将它传递给参数对象,如下所示:

FB.api('/{fb-graph-node-goes-here}/, {
  access_token: "TOKEN GOES HERE"
  //other parameters can go here aswell

}, function(response) {
  console.log(response);
});
于 2016-06-19T19:19:10.830 回答
4

如果用户登录(例如使用 FB.login),则不需要传递令牌。事实上,通过使用 JavaScript SDK(或 PHP SDK),您几乎不需要处理(用户)访问令牌。

因此,您的电话将是这样的:

FB.api('/me/friends', function(response) {
   console.log(response);
});

如果用户安装了应用程序,则获取信息:

于 2013-05-19T17:47:08.437 回答
3

我就是这样做的:

将 access_token 作为参数传递。

FB.api("/me", { access_token : response.authResponse.accessToken }, {fields: ['last_name', 'first_name', 'name']},
        function (response) {
            console.log(response);
            console.log('Name: ' + response.name);
        }
      );
于 2017-06-27T19:45:39.890 回答
0

如果请求扩展字段,您将需要传递访问令牌。请参阅

https://developers.facebook.com/docs/facebook-login/permissions/v2.0#reference-extended-profile

于 2014-07-18T10:33:59.247 回答