如何使用Facebook的SDK机器人获得Facebook的照片,姓名,性别如何使用、机器人、性别、姓名

2023-09-05 09:46:20 作者:笑在眉眼.

我工作的一个Android应用程序中,任何Android的用户是谁在使用我们的应用程序登录到Facebook的,我需要提取他的照片,他的性别,他从Facebook的全称。我使用Facebook的SDK这一点。

I am working on an Android application in which any Android user who is logging to Facebook using our Application, I need to extract his photo, his gender, his full name from the Facebook. I am using Facebook SDK for this.

随着Facebook的SDK中的帮助下,我能够登录到Facebook的,但我不知道如何从Facebook的提取他的照片,性别和全名?

With the help of Facebook SDK, I am able to login into Facebook but I am not sure how to extract his photo,gender and full name from the facebook?

下面是code我使用登录到Facebook上。我跟着这个教程

Below is the code I am using to login into Facebook. I followed this tutorial

public class SessionLoginFragment extends Fragment {

    private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";

    private TextView textInstructionsOrLink;
    private Button buttonLoginLogout;
    private Session.StatusCallback statusCallback = new SessionStatusCallback();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);

    buttonLoginLogout = (Button) view.findViewById(R.id.buttonLoginLogout);
    textInstructionsOrLink = (TextView) view.findViewById(R.id.instructionsOrLink);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {
        if (savedInstanceState != null) {
        session = Session.restoreSession(getActivity(), null, statusCallback,
            savedInstanceState);
        }
        if (session == null) {
        session = new Session(getActivity());
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

    updateView();

    return view;
    }

    @Override
    public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
    }

    private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        Log.d("Hello", URL_PREFIX_FRIENDS + session.getAccessToken());
        Intent i = new Intent(getActivity(), ThesisProjectAndroid.class);
        startActivity(i);

    } else {
        Log.d("Hello", "Login Failed");
        textInstructionsOrLink.setText(R.string.instructions);
        buttonLoginLogout.setText(R.string.login);
        buttonLoginLogout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            onClickLogin();
        }
        });
    }
    }

    private void onClickLogin() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
    } else {
        Session.openActiveSession(getActivity(), this, true, statusCallback);
    }
    }

    private void onClickLogout() {
    Session session = Session.getActiveSession();
    if (!session.isClosed()) {
        session.closeAndClearTokenInformation();
    }
    }

    private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
    }
    }
}

谁能告诉我,我需要进行更改,上面的类来获得所有的三个信息我需要的。据我知道,如果我能得到Facebook的唯一ID的那个人,我可以得到我所猜的信息。有什么想法?

Can anyone tell me where I need to make changes in the above class to get all the three information I needed. As far as I know If I am able to get facebook unique id for that person, I can get all the information I guess. Any thoughts?

推荐答案

在你的 StatusCallback 功能,您可以从 GraphUser 对象

In your StatusCallback function, you can get the details from the GraphUser object

private class SessionStatusCallback implements Session.StatusCallback {
    private String fbAccessToken;

    @Override
    public void call(Session session, SessionState state, Exception exception) {
        updateView();
        if (session.isOpened()) {
            fbAccessToken = session.getAccessToken();
            // make request to get facebook user info
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    Log.i("fb", "fb user: "+ user.toString());

                    String fbId = user.getId();
                    String fbAccessToken = fbAccessToken;
                    String fbName = user.getName();
                    String gender = user.asMap().get("gender").toString();
                    String email = user.asMap().get("email").toString();

                    Log.i("fb", userProfile.getEmail());
                }
            });
        }
    }
}