Facebook SDK访问朋友的生日

阿比乔·萨卡(Abhijoy Sarkar)

请提供示例代码,说明如何通过Facebook SDK访问用户朋友的数据,特别是生日。所有操作都可使用带有Facebook登录名的Android Studio完成。

我是android开发的新手。这个特定的项目基于android和facebook的集成。它需要登录,然后才能访问用户朋友的生日。

public class MainActivityFragment extends Fragment {

private CallbackManager callbackManager;
private TextView textView;

private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback(){
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response){
                        Log.v("LoginActivity",response.toString());

                        String email = object.getString("email");
                        String birthday = object.getString("birthday");
                    }
                });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender,birthday");
        request.setParameters(parameters);
        request.executeAsync();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public MainActivityFragment() {

}


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker= new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

            // Set the access token using
            // currentAccessToken when it's loaded or set.

        }
    };

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            displayMessage(newProfile);
        }
    };

    accessTokenTracker.startTracking();
    profileTracker.startTracking();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    textView = (TextView) view.findViewById(R.id.textView);

    loginButton.setReadPermissions(Arrays.asList(
            "public_profile", "email", "user_birthday", "user_friends"));
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, callback);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);

}

private void displayMessage(Profile profile){
    if(profile != null){
        textView.setText(profile.getName());
    }
}

@Override
public void onStop() {
    super.onStop();
    accessTokenTracker.stopTracking();
    profileTracker.stopTracking();
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    displayMessage(profile);
}

}

卢施恩

无法访问用户朋友的生日。甚至不可能再访问所有用户朋友。您也只能访问获得该user_friends许可授权您的应用程序的朋友如果您想获得他们的生日,他们必须获得user_birthday许可才能授权您的应用

换句话说,您只能获得使用user_friendsuser_birthday许可对您的应用进行授权的用户的生日这将是API端点:/me/friends?fields=name,birthday

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章