Android的Facebook的4.0 SDK如何获得电子邮件,出生日期和性别用户的 [英] Android Facebook 4.0 SDK How to get Email, Date of Birth and gender of User

查看:3549
本文介绍了Android的Facebook的4.0 SDK如何获得电子邮件,出生日期和性别用户的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code。我希望用户的出生日期,电子邮件和性别。请帮忙。如何获取这些数据?

这是我的 onViewCreated()的片段里面。

  @覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){

    //设置的TextView。
    mTextDetails =(TextView中)view.findViewById(R.id.text_details);

    //设置登录按钮。
    LoginButton mButtonLogin =(LoginButton)view.findViewById(R.id.login_button);
    // setFragment只有当你使用它一个片段里。
    mButtonLogin.setFragment(本);
    mButtonLogin.setReadPermissions(user_friends);
    mButtonLogin.setReadPermissions(public_profile);
    mButtonLogin.setReadPermissions(电子邮件);
    mButtonLogin.setReadPermissions(user_birthday);

    //注册一个回调方法,当登录按钮被点击。
    mButtonLogin.registerCallback(mCallbackManager,mFacebookCallback);

}
 

这是我的回调方法。

 私人FacebookCallback< LoginResult> mFacebookCallback =新FacebookCallback< LoginResult>(){
    @覆盖
    公共无效的onSuccess(LoginResult loginResult){
        Log.d(Shreks片段,的onSuccess);


        个人资料个人资料= Profile.getCurrentProfile();
        Log.d(Shreks片段的onSuccess,+轮廓);

        //获取用户名
        mTextDetails.setText(profile.getName()+);

    }


    @覆盖
    公共无效OnCancel的(){
        Log.d(Shreks Fragmnt,OnCancel的」);
    }

    @覆盖
    公共无效onerror的(FacebookException E){
        Log.d(Shreks片段,onError的+ E);
    }
};
 

解决方案

这是不正确的方式来设置的权限。

要覆盖的权限。

替换此。

  mButtonLogin.setReadPermissions(user_friends);
mButtonLogin.setReadPermissions(public_profile);
mButtonLogin.setReadPermissions(电子邮件);
mButtonLogin.setReadPermissions(user_birthday);
 

通过此,该方法setReadPermissions接受一个ArrayList

  loginButton.setReadPermissions(Arrays.asList(public_profile,电子邮件,user_birthday,user_friends));
 

此外,我添加完整的code查询与GraphRequest领域

  // FB
    私人LoginButton loginButton;
    CallbackManager callbackManager;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_login);

        loginButton =(LoginButton)findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList(public_profile,电子邮件,user_birthday));

        callbackManager = CallbackManager.Factory.create();

        //回调注册
        loginButton.registerCallback(callbackManager,新FacebookCallback< LoginResult>(){
            @覆盖
            公共无效的onSuccess(LoginResult loginResult){
                //应用code
                GraphRequest请求= GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        新GraphRequest.GraphJSONObjectCallback(){
                            @覆盖
                            公共无效onCompleted(
                                    的JSONObject对象,
                                    GraphResponse响应){
                                //应用code
                                Log.v(LoginActivity,response.toString());
                            }
                        });
                捆绑参数=新包();
                parameters.putString(域,编号,姓名,电子邮箱,性别,生日);
                request.setParameters(参数);
                request.executeAsync();


            }

            @覆盖
            公共无效OnCancel的(){
                //应用code
                Log.v(LoginActivity,取消);
            }

            @覆盖
            公共无效onerror的(FacebookException除外){
                //应用code
                Log.v(LoginActivity,exception.getCause()的toString());
            }
        });


    }
 

编辑:

一个可能的问题是,Facebook的假定你的电子邮件无效,对其进行测试,使用<一个href="https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname%2Cemail&">Graph API浏览器并设法得到它。如果即使在那里你不能得到你的电子邮件,在您的个人资料设置进行更改,然后再试一次。对于一些开发商评论我的回答这个方法解决了这个问题。

希望它能帮助:D

I am using the following code. I want the user's Date Of Birth, Email and Gender. Please help. How to retrieve those data?

This is my onViewCreated() inside the Fragment.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    // Setup TextView.
    mTextDetails = (TextView) view.findViewById(R.id.text_details);

    // Set up Login Button.
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button);
    // setFragment only if you are using it inside a Fragment.
    mButtonLogin.setFragment(this);
    mButtonLogin.setReadPermissions("user_friends");
    mButtonLogin.setReadPermissions("public_profile");
    mButtonLogin.setReadPermissions("email");
    mButtonLogin.setReadPermissions("user_birthday");

    // Register a callback method when Login Button is Clicked.
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);

}

This is my Callback Method.

private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Log.d("Shreks Fragment", "onSuccess");


        Profile profile = Profile.getCurrentProfile();
        Log.d("Shreks Fragment onSuccess", "" +profile);

        // Get User Name
        mTextDetails.setText(profile.getName() + "");

    }


    @Override
    public void onCancel() {
        Log.d("Shreks Fragmnt", "onCancel");
    }

    @Override
    public void onError(FacebookException e) {
        Log.d("Shreks Fragment", "onError " + e);
    }
};

解决方案

that's not the right way to set the permissions.

you are overwriting the permissions.

Replace this.

mButtonLogin.setReadPermissions("user_friends");
mButtonLogin.setReadPermissions("public_profile");
mButtonLogin.setReadPermissions("email");
mButtonLogin.setReadPermissions("user_birthday");

With this, the method setReadPermissions accepts an ArrayList

loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));

Also i add complete code to query fields with GraphRequest

//fb
    private LoginButton loginButton;
    CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginButton = (LoginButton) findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday"));

        callbackManager = CallbackManager.Factory.create();

        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                Log.v("LoginActivity", response.toString());
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();


            }

            @Override
            public void onCancel() {
                // App code
                Log.v("LoginActivity", "cancel");
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.v("LoginActivity", exception.getCause().toString());
            }
        });


    }

EDIT:

One possible problem is that Facebook assumes that your email is invalid, to test it, use the Graph API Explorer and try to get it. If even there you can't get your email, change it in your profile settings and try again. This approach resolved this issue for some developers commenting my answer.

Hope it helps :D

这篇关于Android的Facebook的4.0 SDK如何获得电子邮件,出生日期和性别用户的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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