使用 Graph API 的 Facebook“ Friends.getAppUsers”

我有一个应用程序,它使用旧的 REST API 调用 Friends.getAppUsers来获取当前已经授权我的应用程序的用户的朋友列表。

我已经阅读了文档,如何使用 GraphAPI 完成这项工作?这方面的一个例子是什么?

66024 次浏览

Yes, the Graph API is horribly documented. I don't see any documentation for an "application" type, but they do reference an application's info in the docs:

https://graph.facebook.com/2439131959

So, perhaps you can get the application's members using the Group syntax?

https://graph.facebook.com/2439131959/members

You'll need an authenticated session to test it. Otherwise, it looks like Facebook is pushing us to use FQL to send queries directly:

http://developers.facebook.com/docs/reference/fql/application

You can execute FQL queries by fetching https://api.facebook.com/method/fql.query?query=QUERY. You can specify a response format as either XML or JSON with the format query parameter.

So you can pass a FQL query to get information about your application.

I've done a lot of investigations on this issue. From what I can tell, there is no way to do Friends.getAppUsers using Graph API. The latest SDKs provided by Facebook all use the old REST API to get friends using the application.

The workaround is to do the filtering yourself. Presumably, you have all the uid's of the users who have signed up for your application sitting in your own database. So first get all the users' friends' uids, and select the users from your database who have matching uids.

The new Facebook Graph API program seems very poorly executed and not quite thought through. It seems they rushed to publish it for Facebook f8 before it was mature, and lots of functionality is missing that was available before.

This can be done with FQL.

SELECT uid FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ?)
AND is_app_user = 1

QED!

Old REST API:

$facebook->api_client->friends_getAppUsers();

New Graph API:

$facebook->api(array('method' => 'friends.getAppUsers'));

You can still call the old REST API's in the new Graph API. That is prefectly valid.

An alternative way is to use FQL to get the application user's friends.

I'm not sure if there's a way to do this using just Graph API.

I searched around for a while, and I too thought this wasn't possible using Graph API.

However, I posted a similar question, Replacement for old GetAppUsers call to see a user's friends who use my application?, for the specific API I was using and was given a great general answer.

https://graph.facebook.com/me/friends?fields=installed

or more generally

https://graph.facebook.com/{user id}/friends?fields=installed

This returns all friends, with the additional field "installed=true" for those friends who use the application.

Here is it working in the Graph API Explorer:

Using restFB, I did the following (thanks Igy / Alex for the guidance). Note that Facebook returns an array of friend IDs with "installed"=true if the user is installed (as can be seen here).

First extend the User class, adding an installed field:

import com.restfb.Facebook;
import com.restfb.types.User;


public class InstalledUser extends User {


@Facebook
private boolean installed;


public InstalledUser() {
}


public boolean getInsatlled() {
return installed;
}
}

Next, using the DefaultFacebookClient:

FacebookClient facebook = new DefaultFacebookClient(pFacebookAccessToken);
Connection<InstalledUser> installedFacebookUsers = facebook.fetchConnection("/" + pFacebookId + "/friends", InstalledUser.class, Parameter.with("fields", "installed"));
for (List<InstalledUser> friends : installedFacebookUsers) {
for (InstalledUser friend : friends) {
if (friend.getInsatlled()) {
// Add friend.getId() to a list of ID, or whatever
}
}
}