Getting email from Facebook - FB android SDK

前端 未结 1 987
你的背包
你的背包 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() {
            @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);
    
    
    

    0 讨论(0)
提交回复
热议问题