Facebook sdk访问朋友的生日

时间:2016-09-04 18:57:52

标签: android facebook facebook-graph-api

请举例说明如何访问用户朋友的数据,特别是通过Facebook SDK生日。所有这一切都要使用Android Studio完成,并登录Facebook。

我是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);
}

}

1 个答案:

答案 0 :(得分:0)

无法访问用户朋友的生日。甚至不可能再访问所有用户朋友。您也只能通过user_friends权限访问授权您的应用的朋友。如果您想要过生日,他们必须使用user_birthday权限授权您的应用。

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