gpt4 book ai didi

android - 我如何从 FaceBook 获取好友列表?

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:02 27 4
gpt4 key购买 nike

我在 Stack Overflow 中看到了很多这样的问题,但它们并没有解决我的问题。我提到了this , this以及文档链接,如 thisthis


我使用 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}}}

最佳答案

试试这个:

权限:

setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));

这个变量用于处理你的 friend :

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;
}
}
}

关于android - 我如何从 FaceBook 获取好友列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34117115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com