如何使用Facebook Graph API获取用户相册列表?
答案 0 :(得分:0)
在活动的OnCreate ..
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken token = loginResult.getAccessToken();
GraphRequest.Callback callback = new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
//Use the data
}
};
GraphRequest req = GraphRequest.newGraphPathRequest(token, "me/albums", callback);
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, count");
req.setParameters(parameters);
req.executeAsync();
}
@Override
public void onCancel() {
//User Cancelled
}
@Override
public void onError(FacebookException e) {
//Handle Error
}
});
在OnClick方法中:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_photos"));
你还需要这个:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}