2016-06-15 3 views
-1

私のアプリケーションにFacebookログインを実装しています。しかし、私はユーザーの電子メールIDを取得していません。私はユーザー名やその他の詳細を取得していますが、電子メールIDは取得していません。Facebookのログインを使用しているユーザーの電子メールIDを取得していません

これは私のコード

のGradleです:のonCreate(中

compile 'com.facebook.android:facebook-android-sdk:4.9.0' 

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を取得していません。

答えて

0

logInWithReadPermissionsに電子メールの許可を追加します。

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

おかげで、この、あなたがそれを使用したいときに、ユーザーの電子メールを得るかするので、どのように:) – Priyanka

0

あなたは、おそらく1つの以上の権限emailを追加する必要があります。
だから、ここにあなたが行く、

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

を働いていましたか? – ArdenDev

+0

@IphoneDeveloperあなたはFacebookCallback からメールを受け取ります。質問のコードを確認してください。 –

関連する問題