本文介绍了从Facebook获取电子邮件 - FB android SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从Facebook获取用户的电子邮件和名称

I'm using the following code to get email and name of the user from facebook

protected void onCreate(Bundle savedInstanceState) {

    //Display page
    setContentView(R.layout.activity_main);
    AppEventsLogger.activateApp(MainActivity.this);

    FBloginButton = (Button) findViewById(R.id.fb_login_button);
    callbackManager = CallbackManager.Factory.create();

    FBloginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        //Login the user through facebook when button is clicked
            onFblogin();
        }
    });

}

private void onFblogin() {
    callbackManager = CallbackManager.Factory.create();

    // Set permissions
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_friends", "public_profile"));

    //Try logging in the user
    LoginManager.getInstance().registerCallback(callbackManager, 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.i("LoginActivity", response.toString());

                    // Get facebook data from login
                    try {
                        email = object.getString("email");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    try {
                        name = object.getString("name");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //new fbLogin().execute();

                }
            });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "email, name");
            request.setParameters(parameters);
            request.executeAsync();

        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException error) {

        }
    });
}

但是,这只会返回用户名,而不是用户的电子邮件。我如何得到这两个?

However, this only returns the name and not the email of the user. How can I get both?

推荐答案

我的工作方法如下:

public void getUserDetailsFromFB(AccessToken accessToken) {

    GraphRequest req=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {
            Toast.makeText(getApplicationContext(),"graph request completed",Toast.LENGTH_SHORT).show();
            try{
                String email =  object.getString("email");
                String birthday = object.getString("birthday");
                String gender = object.getString("gender");
                String name = object.getString("name");
                String id = object.getString("id");
                String photourl =object.getJSONObject("picture").getJSONObject("data").getString("url");

            }catch (JSONException e)
            {
                Toast.makeText(getApplicationContext(),"graph request error : "+e.getMessage(),Toast.LENGTH_SHORT).show();

            }

        }
    });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)");
    req.setParameters(parameters);
    req.executeAsync();
}

然后在您的LoginButton中:

Then in your LoginButton :

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {
            getUserDetailsFromFB(loginResult.getAccessToken());

        }
        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(),"fb user canceled",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onError(FacebookException e) {
            Toast.makeText(getApplicationContext(),"fb error",Toast.LENGTH_SHORT).show();
        }
    });

初始化为:

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

<com.facebook.login.widget.LoginButton
                android:id="@+id/facebook_login_button"
                xmlns:facebook="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                facebook:com_facebook_login_text="Fb login button"
                android:layout_gravity="center_horizontal|bottom"
                android:textSize="20dp"
                android:layout_centerHorizontal="true" />

这篇关于从Facebook获取电子邮件 - FB android SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:56