如何获取Facebook照片,全名,性别使用Facebook SDK android [英] How to get Facebook photo, full name, gender using Facebook SDK android

查看:169
本文介绍了如何获取Facebook照片,全名,性别使用Facebook SDK android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android应用程序,其中任何使用我们的应用程序登录到Facebook的Android用户,我需要从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?

下面是我用来登录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());
                }
            });
        }
    }
}

这篇关于如何获取Facebook照片,全名,性别使用Facebook SDK android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆