Getting email from Facebook - FB android SDK

前端 未结 1 986
你的背包
你的背包 2021-01-23 04:48

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

protected void onCreate(Bundle savedInstanceState) {

    //Display page
    setCon         


        
相关标签:
1条回答
  • 2021-01-23 05:24

    My working method like :

    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();
    }
    

    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();
            }
        });
    

    Init like :

    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" />
    
    0 讨论(0)
提交回复
热议问题