使用facebook登录

时间:2016-06-15 06:32:57

标签: android facebook

我正在我的应用程序中实现Facebook登录。但我没有收到用户的电子邮件ID。我收到了用户名和其他详细信息,但没有收到电子邮件ID。

这是我的代码

的gradle:

 compile 'com.facebook.android:facebook-android-sdk:4.9.0'
onCreate()

中的

 btn_facebook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "user_friends"));
        }
    });

     ///Facebook login
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(LoginResult loginResult) {

                    System.out.println("onSuccess");
                    progressDialog = new ProgressDialog(LoginActivity.this);
                    progressDialog.setMessage("Processing request.......");
                    progressDialog.show();
                    String accessToken = loginResult.getAccessToken().getToken();
                    Log.i("accessToken", accessToken);

                    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
                            Bundle bFacebookData = getFacebookData(object);


                        }

                    });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
                    request.setParameters(parameters);
                    request.executeAsync();

                    progressDialog.dismiss();

                    SessionHandler handler = new SessionHandler(LoginActivity.this.getApplicationContext());
                    handler.storeLoginSession(str_email, logintype, str_email,str_email);

                    SharedPreferences.Editor editor = getSharedPreferences(
                            "UserId", MODE_PRIVATE).edit();
                    editor.putString("id", str_email);
                    editor.commit();


                    if (logintype.equals("hirer")) {
                        Intent i = new Intent(LoginActivity.this, HirerDashboard.class);
                        i.putExtra("logintype", "hirer");
                        startActivity(i);
                    } else if (logintype.equals("worker")) {
                        Intent i = new Intent(LoginActivity.this, WorkerDashboard.class);
                        i.putExtra("logintype", "worker");
                        startActivity(i);
                    }
                }

                @Override
                public void onCancel() {
                    System.out.println("onCancel");
                }

                @Override
                public void onError(FacebookException exception) {
                    System.out.println("onError");
                    Log.v("LoginActivity", exception.getCause().toString());
                }
            });

getFacebookData()

  private Bundle getFacebookData(JSONObject object) {

    try {
        String id = "";
        Bundle bundle = new Bundle();
        try {
            id = object.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
            Log.i("profile_pic", profile_pic + "");
            bundle.putString("profile_pic", profile_pic.toString());

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }

        bundle.putString("idFacebook", id);
        try {
            if (object.has("first_name")) {
                bundle.putString("first_name", object.getString("first_name"));
                str_email = "" + object.getString("first_name");
                Log.e("first_name", "" + object.getString("first_name"));
            }

            if (object.has("last_name")) {
                bundle.putString("last_name", object.getString("last_name"));
                Log.e("last_name", "" + object.getString("last_name"));
            }


            if (object.has("email")) {
                bundle.putString("email", object.getString("email"));
                Log.e("email", "" + object.getString("email"));
            }

            if (object.has("gender"))
                bundle.putString("gender", object.getString("gender"));
            if (object.has("birthday"))
                bundle.putString("birthday", object.getString("birthday"));
            if (object.has("location"))
                bundle.putString("location", object.getJSONObject("location").getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return bundle;
    } finally {

    }
}

请帮助我..为什么我没有收到电子邮件ID。

2 个答案:

答案 0 :(得分:0)

  

在logInWithReadPermissions中添加电子邮件权限。

LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "email","user_friends"));

答案 1 :(得分:0)

您可能需要再添加一个权限email
所以,你走了,

LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "user_friends", "email"));