我在Stack Overflow中看到了很多这样的问题,但是他们没有解决我的问题。我提到了this,this以及文档链接,例如this和this
我使用LoginButton
登录我的应用程序。
我能够使用以下代码获取用户(已登录)姓名,电子邮件等(它工作正常):
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
email = object.getString("email");
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("EMAIL",email);
Log.e("GraphResponse", "-------------" + response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,link,gender,birthday,email");
request.setParameters(parameters);
request.executeAsync();
}
有权限:
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, callback);
我在LogCat中获得了JSON。但现在我想获得朋友列表,所以我通过查看文档编写代码并稍微更改了我的代码,如下所示:
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
// I saw following code in Documentation
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{friendlist-id}", /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
// handle the result
Log.d("RESPONSE KBT",response.toString());
}
}
).executeAsync();
}
有权限:
loginButton.setReadPermissions(Arrays.asList("email", "user_friends","read_custom_friendlists"));
loginButton.registerCallback(callbackManager, callback);
我在LogCat中得到了这个:
RESPONSE KBT﹕ {Response: responseCode: 404, graphObject: null, error: {HttpStatus: 404, errorCode: 803, errorType: OAuthException, errorMessage: (#803) Some of the aliases you requested do not exist: {friendlist-id}}}
答案 0 :(得分:4)
试试这个:
<强>许可:强>
setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
此变量用于处理您的朋友:
public static List<TaggableFriends> friendListFacebook = new ArrayList<TaggableFriends>();
这种获取朋友的方法(在登录类的onSuccess()
方法中调用它):
public void getFriends()
{
if(AccessToken.getCurrentAccessToken() != null)
{
GraphRequest graphRequest = GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
"me/taggable_friends",
new GraphRequest.Callback()
{
@Override
public void onCompleted(GraphResponse graphResponse)
{
if(graphResponse != null)
{
JSONObject jsonObject = graphResponse.getJSONObject();
String taggableFriendsJson = jsonObject.toString();
Gson gson = new Gson();
TaggableFriendsWrapper taggableFriendsWrapper= gson.fromJson(taggableFriendsJson, TaggableFriendsWrapper.class);
ArrayList<TaggableFriends> invitableFriends = new ArrayList<TaggableFriends>();
invitableFriends = taggableFriendsWrapper.getData();
int i;
for(i = 0; i < invitableFriends.size(); i++)
{
try
{
friendListFacebook.add(invitableFriends.get(i));
}
catch(Exception e){}
}
}else {
}
}
}
);
Bundle parameters = new Bundle();
parameters.putInt("limit", 5000); //5000 is maximum number of friends you can have on Facebook
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
}
添加此课程:
<强> TaggableFriendsWrapper:强>
public class TaggableFriendsWrapper {
private ArrayList<TaggableFriends> data;
private Paging paging;
public ArrayList<TaggableFriends> getData() {
return data;
}
public void setData(ArrayList<TaggableFriends> data) {
this.data = data;
}
public Paging getPaging() {
return paging;
}
public void setPaging(Paging paging) {
this.paging = paging;
}
public class Paging {
private Cursors cursors;
public Cursors getCursors() {
return cursors;
}
public void setCursors(Cursors cursors) {
this.cursors = cursors;
}
}
public class Cursors {
private String after;
private String before;
public String getAfter() {
return after;
}
public void setAfter(String after) {
this.after = after;
}
public String getBefore() {
return before;
}
public void setBefore(String before) {
this.before = before;
}
}
}
<强> TaggableFriends:强>
public class TaggableFriends {
private String id;
private String name;
private Picture picture;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
public class Picture {
private Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
public class Data {
private String url;
private boolean is_sillhouette;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public boolean isIs_sillhouette() {
return is_sillhouette;
}
public void setIs_sillhouette(boolean is_sillhouette) {
this.is_sillhouette = is_sillhouette;
}
}
}
答案 1 :(得分:2)
获取授权用户的朋友的正确API端点为/me/friends
,您需要使用user_friends
权限进行授权。请注意,您只会获得使用user_friends
授权您的应用的朋友。
您只能访问所有朋友进行标记(使用/me/taggable_friends
)或邀请朋友加入使用Canvas实施的游戏(使用/me/invitable_friends
)。
更多信息:Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
答案 2 :(得分:0)
尝试不使用Brackets {}
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friendlist-id", /* I actually tried ,friend-list-id' , /me/friends' , '/me/taggable_Friends' and many*/
null, ...... );