我正在开发使用facebook sdk的新Android应用程序。我想获取用户朋友列表,我这样做:
Request request = Request.newMyFriendsRequest(
Session.getActiveSession(),
new Request.GraphUserListCallback() {
public void onCompleted(List<GraphUser> paramAnonymousList,
Response paramAnonymousResponse) {
Toast.makeText(getApplicationContext(),
paramAnonymousList.toString(),
Toast.LENGTH_SHORT).show();
Log.e(TAG, paramAnonymousList.toString()
+ paramAnonymousResponse.toString());
}
});
request.executeAsync();
然而,当我运行应用程序时,我会得到类似的东西:
GraphObjectList{itemType=GraphUser, state=[]}{Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}
我试图在不在开发模式的应用程序中运行此代码并且它工作正常 - 我能够获取用户的数据。可能是响应代码200的原因是什么。根据:https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0响应代码200属于facebook权限错误。但是要获取用户的朋友列表,我不必提供任何特定权限,那么这个响应的原因是什么?
提前致谢
答案 0 :(得分:2)
HTTP 200表示您的请求正常。
真正的问题是Facebook API v2.0。应用无法检索用户的完整朋友列表,只有已使用该应用的朋友。即使您的应用程序仍在v1.0中,5月1日之后首次登录的用户也会获得v2.0行为。
参考:https://developers.facebook.com/docs/graph-api/reference/v2.0/user/friends
干杯!
答案 1 :(得分:0)
我已经这样做了,并且在Facebook SDK 4.18.0
中完美运行public class SignIn extends AppCompatActivity {
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//before set conteview
FacebookSdk.sdkInitialize(getApplicationContext());
// AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_signin);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest graphRequest=GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
Log.d("Graph Response",graphResponse.toString());
String myCustomizedResponse = graphResponse.getJSONObject().toString();
Log.d("Ketan_Ramani",graphResponse.getJSONObject().toString());
try {
JSONObject obj = new JSONObject(myCustomizedResponse);
String id = obj.getString("id");
String first_name = obj.getString("first_name");
String last_name = obj.getString("last_name");
String email = obj.getString("email");
Log.d("Id",id);
Log.d("FirstName",first_name);
Log.d("LastName",last_name);
Log.d("Email",email);
} catch (JSONException e) {
Utils.hide_dialog();
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,email");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}