Android Facebook SDK 4.X , how to get Email address and Facebook Access Token to pass it to Web Service

前端 未结 3 1521
情深已故
情深已故 2020-12-09 00:07

EDIT : My Question is how to get Email , UserId , Facebook Authentication with Facebook SDK 4.X , at this moment , with Ming Respond , i know how can i get Email , User Id

相关标签:
3条回答
  • 2020-12-09 00:45

    Easiest answer that i found after hours of searching is as follows,the highest voted answer didn't work for me and email was alway empty

    LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object,GraphResponse response) {
    
                                JSONObject json = response.getJSONObject();
                                try {
                                    if(json != null){
                                        String text = json.getString("email");
                                        Log.d("email",text);
    
                                    }
    
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,link,email,picture");
                        request.setParameters(parameters);
                        request.executeAsync();
    
                    }
    
                    @Override
                    public void onCancel() {
                        // App code
                    }
    
                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                    }
                });
    

    I believe setting parameters as required fields to the Graph Request is a crucial thing here. You can also use this code with LoginButton, works without issue.

    ****** Update******** After using this code with many accounts found that if the email isn't verified it won't be returned, in such case following code helped along with abo

    facebookLoginButton.setReadPermissions("email");
    

    Hope this helps further

    0 讨论(0)
  • 2020-12-09 00:53
    fbLoginBtn.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest.newMeRequest(
                loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject me, GraphResponse response) {
                        if (response.getError() != null) {
                            // handle error
                        } else {
                            String email = me.optString("email");
                            String id = me.optString("id");
                            // send email and id to your web server
                        }
                    }
                }).executeAsync();
        }
    
        @Override
        public void onCancel() {
            // App code
        }
    
        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });
    
    0 讨论(0)
  • 2020-12-09 00:53

    I must add extra field for user Email.

        //register facebook login callback
        LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>()
        {
            @Override
            public void onSuccess (LoginResult loginResult)
            {
                Log.d(TAG, "FB: login success");
                showLoading(true);
    
                final String token = loginResult.getAccessToken().getToken();
    
                //prepare fields with email
                String[] requiredFields = new String[]{"email"};
                Bundle parameters = new Bundle();
                parameters.putString("fields", TextUtils.join(",", requiredFields));
    
                GraphRequest requestEmail = new GraphRequest(loginResult.getAccessToken(), "me", parameters, null, new GraphRequest.Callback()
                {
                    @Override
                    public void onCompleted (GraphResponse response)
                    {
                        if (response != null)
                        {
                            GraphRequest.GraphJSONObjectCallback callbackEmail = new GraphRequest.GraphJSONObjectCallback()
                            {
                                @Override
                                public void onCompleted (JSONObject me, GraphResponse response)
                                {
                                    if (response.getError() != null)
                                    {
                                        Log.d(TAG, "FB: cannot parse email");
                                        showDialog(getString(R.string.dialog_message_unknown_error));
                                        showLoading(false);
                                    }
                                    else
                                    {
                                        String email = me.optString("email");
    
                                        // send email and id to your web server
                                        //...
                                    }
                                }
                            };
    
                            callbackEmail.onCompleted(response.getJSONObject(), response);
                        }
                    }
                });
    
                requestEmail.executeAsync();
            }
    
            @Override
            public void onCancel ()
            {
                Log.d(TAG, "FB: login cancel");
                showDialog(getString(R.string.dialog_message_unknown_error));
            }
    
            @Override
            public void onError (FacebookException e)
            {
                Log.d(TAG, "FB: login error " + e.getMessage());
                showDialog(getString(R.string.dialog_message_unknown_error));
            }
        });
    
    0 讨论(0)
提交回复
热议问题