Facebook Graph API v2.0+-/me/friends返回空值,或者只返回同时使用我的应用程序的朋友

2024-09-24 22:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Graph API v2.0获取我的朋友姓名和ID,但数据返回空:

{
  "data": [
  ]
}

当我使用v1.0时,以下请求一切正常:

FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                              NSDictionary* result,
                                              NSError *error) {
    NSArray* friends = [result objectForKey:@"data"];
    NSLog(@"Found: %i friends", friends.count);
    for (NSDictionary<FBGraphUser>* friend in friends) {
        NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
    }
}];

但现在我找不到朋友了!


Tags: 数据friendapiiddata朋友resultgraph
3条回答

尽管Simon Cross的答案被接受并且是正确的,但我想我可以用一个例子(Android)来说明需要做什么。我会尽我所能保持它的概括性,只关注这个问题。就我个人而言,我将东西存储在一个数据库中,因此加载是平滑的,但这需要一个CursorAdapter和ContentProvider,这有点超出了这里的范围。

我自己来到这里,然后想,现在怎么了?!

问题

就像user3594351一样,我注意到朋友的数据是空白的。我是用FriendPickerFragment发现的。三个月前起作用的,现在不起作用了。甚至连Facebook的例子都被打破了。所以我的问题是“如何手工创建FriendPickerFragment?”?

什么不起作用

Simon Cross中的选项1不够强大,无法邀请朋友加入应用程序。Simon Cross也推荐了Requests对话框,但一次只允许五个请求。在任何给定的Facebook登录会话中,“请求”对话框也显示相同的朋友。没用。

什么有效(摘要)

选择2和一些艰苦的工作。你必须确保你符合Facebook的新规则:1)你是一个游戏2)你有一个画布应用(网络状态)3)你的应用已在Facebook注册。这都是在Facebook开发者网站的设置下完成的。

为了在我的应用程序中手动模拟好友选取器,我执行了以下操作:

  1. 创建一个显示两个片段的选项卡活动。每个片段都显示一个列表。一个片段表示可用的朋友(/me/friends),另一个片段表示不可邀请的朋友(/me/invitable\u friends)。使用相同的片段代码呈现两个选项卡。
  2. 创建一个异步任务,从Facebook获取好友数据。加载数据后,将其丢到适配器,适配器将把值呈现到屏幕上。

详细信息

异步任务

private class DownloadFacebookFriendsTask extends AsyncTask<FacebookFriend.Type, Boolean, Boolean> {

    private final String TAG = DownloadFacebookFriendsTask.class.getSimpleName();
    GraphObject graphObject;
    ArrayList<FacebookFriend> myList = new ArrayList<FacebookFriend>();

    @Override
    protected Boolean doInBackground(FacebookFriend.Type... pickType) {
        //
        // Determine Type
        //
        String facebookRequest;
        if (pickType[0] == FacebookFriend.Type.AVAILABLE) {
            facebookRequest = "/me/friends";
        } else {
            facebookRequest = "/me/invitable_friends";
        }

        //
        // Launch Facebook request and WAIT.
        //
        new Request(
            Session.getActiveSession(),
            facebookRequest,
            null,
            HttpMethod.GET,
            new Request.Callback() {
                public void onCompleted(Response response) {
                    FacebookRequestError error = response.getError();
                    if (error != null && response != null) {
                        Log.e(TAG, error.toString());
                    } else {
                        graphObject = response.getGraphObject();
                    }
                }
            }
        ).executeAndWait();

        //
        // Process Facebook response
        //
        //
        if (graphObject == null) {
            return false;
        }

        int numberOfRecords = 0;
        JSONArray dataArray = (JSONArray) graphObject.getProperty("data");
        if (dataArray.length() > 0) {

            // Ensure the user has at least one friend ...
            for (int i = 0; i < dataArray.length(); i++) {

                JSONObject jsonObject = dataArray.optJSONObject(i);
                FacebookFriend facebookFriend = new FacebookFriend(jsonObject, pickType[0]);

                if (facebookFriend.isValid()) {
                    numberOfRecords++;

                    myList.add(facebookFriend);
                }
            }
        }

        // Make sure there are records to process
        if (numberOfRecords > 0){
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void onProgressUpdate(Boolean... booleans) {
        // No need to update this, wait until the whole thread finishes.
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            /*
            User the array "myList" to create the adapter which will control showing items in the list.
             */

        } else {
            Log.i(TAG, "Facebook Thread unable to Get/Parse friend data. Type = " + pickType);
        }
    }
}

我创建的FacebookFriend类

public class FacebookFriend {

    String facebookId;
    String name;
    String pictureUrl;
    boolean invitable;
    boolean available;
    boolean isValid;
    public enum Type {AVAILABLE, INVITABLE};

    public FacebookFriend(JSONObject jsonObject, Type type) {
        //
        //Parse the Facebook Data from the JSON object.
        //
        try {
            if (type == Type.INVITABLE) {
                //parse /me/invitable_friend
                this.facebookId =  jsonObject.getString("id");
                this.name = jsonObject.getString("name");

                // Handle the picture data.
                JSONObject pictureJsonObject = jsonObject.getJSONObject("picture").getJSONObject("data");
                boolean isSilhouette = pictureJsonObject.getBoolean("is_silhouette");
                if (!isSilhouette) {
                    this.pictureUrl = pictureJsonObject.getString("url");

                } else {
                    this.pictureUrl = "";
                }

                this.invitable = true;
            } else {
                // Parse /me/friends
                this.facebookId =  jsonObject.getString("id");
                this.name = jsonObject.getString("name");
                this.available = true;
                this.pictureUrl = "";
            }

            isValid = true;
        } catch (JSONException e) {
            Log.w("#", "Warnings - unable to process Facebook JSON: " + e.getLocalizedMessage());
        }
    }
}

在图形API的v2.0中,调用/me/friends返回同时使用该应用程序的用户的朋友。

此外,在v2.0中,必须向每个用户请求user_friends权限。user_friends在默认情况下不再包含在每个登录中。每个用户都必须授予user_friends权限,才能出现在对/me/friends的响应中。有关详细信息,请参见the Facebook upgrade guide,或查看下面的摘要。

如果要使用好友访问非应用程序列表,有两个选项:

  1. If you want to let your people tag their friends在他们使用您的应用程序发布到Facebook的文章中,您可以使用/me/taggable_friendsAPI。Use of this endpoint requires review by Facebook并且只应用于呈现朋友列表以便用户在帖子中标记他们的情况。

  2. If your App is a Game AND your Game supports Facebook Canvas,您可以使用/me/invitable_friends端点来呈现a custom invite dialog,然后将此API返回的令牌传递给the standard Requests Dialog.

在其他情况下,应用程序将无法检索用户朋友的完整列表(仅限那些使用user_friends权限专门授权您的应用程序的朋友)。This has been confirmed by Facebook as 'by design'.

对于希望允许人们邀请朋友使用应用程序的应用程序,您仍然可以使用Send Dialog on Web或新的Message Dialog on iOSAndroid

更新:Facebook在这里发布了一个关于这些更改的常见问题解答:https://developers.facebook.com/docs/apps/faq,它解释了开发人员可以使用的所有选项,以便邀请朋友等

Facebook现在已经修改了他们的政策。如果你的应用程序没有画布实现,或者你的应用程序不是游戏,那么无论如何你都无法获得整个好友列表。当然也有可标记的朋友,但那一个只用于标记。

您将能够拉仅授权应用程序的朋友列表。

使用GraphAPI1.0的应用程序将工作到2015年4月30日,之后将被弃用。

有关详细信息,请参见以下内容:

相关问题 更多 >