簡體   English   中英

Android-使用Facebook SDK 4登錄不會檢索電子郵件

[英]Android - login with facebook SDK 4 dont retrieve email

我正在使用Facebook登錄的項目中工作,我需要在注冊時獲取電子郵件地址

返回的json

{Response:  responseCode: 200, graphObject: {"name":"Hany Bahaa","id":"218274898510181"}, error: null}

我的java代碼

private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.signin);
        ButterKnife.bind(this);

        LoginButton _loginfb = (LoginButton) findViewById(R.id.fb_login);
        _loginfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fblogin();
            }
        });


    }


    private void Fblogin() {
            // Set permissions
            LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("email", "public_profile", "user_friends"));

            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {

                            System.out.println("Success");
                            GraphRequest.newMeRequest(
                                    loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                        @Override
                                        public void onCompleted(JSONObject json, GraphResponse response) {
                                               if (response.getError() != null) {
                                                    // handle error
                                                    Log.i(TAG, "ERROR");
                                            } else {
                                                Log.i(TAG, "Success");
                                                Profile _profile = Profile.getCurrentProfile();
                                                String jsonresult = String.valueOf(json);
                                                System.out.println("JSON Result" + jsonresult);
                                                String str_email = json.optString("email");
                                                System.out.println("JSON Result email" + str_email);
                                                String str_id = json.optString("id");
                                                String str_name = json.optString("name");


                                            }
                                        }
                                    }).executeAsync();
                        }

                        @Override
                        public void onCancel() {
                            Log.d(TAG, "On cancel");
                        }

                        @Override
                        public void onError(FacebookException error) {
                            Log.d(TAG, error.toString());
                        }
                    });
        }


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

您需要使用要檢索的參數將Bundle傳遞給GraphRequest

Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location");

GraphRequest graph = GraphRequest.newMeRequest(
        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject json, GraphResponse response) {
                if (response.getError() != null) {
                    // handle error
                    Log.i(TAG, "ERROR");
                } else {
                    Log.i(TAG, "Success");
                    Profile _profile = Profile.getCurrentProfile();
                    String jsonresult = String.valueOf(json);
                    System.out.println("JSON Result" + jsonresult);
                    String str_email = json.optString("email");
                    System.out.println("JSON Result email" + str_email);
                    String str_id = json.optString("id");
                    String str_name = json.optString("name");


                }
            }
        });

graph.setParameters(parameters);
graph.executeAsync();   

您還可以將Bundle作為GraphRequest構造函數的參數傳遞。

https://developers.facebook.com/docs/reference/android/current/class/GraphRequest/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM