2014-03-26 105 views
4

我想從ParseFacebook的用戶的電子郵件地址爲Android但我越來越null,當我嘗試獲取電子郵件。 這裏是我的代碼如何獲取用戶電子郵件地址使用解析Facebook的Android-

//on Login: 

List<String> permissions = Arrays.asList("email","basic_info", "user_about_me", "user_location"); 
     ParseFacebookUtils.logIn(permissions,SignUpActivity. this, new LogInCallback() { 
      @Override 
      public void done(ParseUser user, ParseException err) { 
       SignUpActivity.this.progressDialog.dismiss(); 
       if(err!=null) 
       Log.w("parse exception",""+err.getMessage()); 
       if (user == null) { 
       Log.e("FB","Uh oh. The user cancelled the Facebook login."); 
       } else if (user.isNew()) { 
        Log.w("FB","User signed up and logged in through Facebook!"); 
        //showUserDetailsActivity(); 
        getProfileInfo(); 
       } else { 
        Log.w("FB","User logged in through Facebook!"); 
       // showUserDetailsActivity(); 
        getProfileInfo(); 
       } 
      } 

     }); 

作出要求:

Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() { 
        @Override 
        public void onCompleted(GraphUser user, Response response) { 
         if (user != null) { 
          JSONObject userProfile = new JSONObject(); 
          try { 
           // Populate the JSON object 
           try { 
            userProfile.put("facebookId", user.getId()); 
            userProfile.put("name", user.getName()); 
           } catch (Exception e) { 
            // TODO: handle exception 
           } 
           if (user.getProperty("email") != null) { 
            userProfile.put("email",(String) user.getProperty("email")); 
           } 
           if (user.getLocation().getProperty("name") != null) { 
            userProfile.put("location", (String) user.getLocation().getProperty("name")); 
           } 
           if (user.getProperty("gender") != null) { 
            userProfile.put("gender",(String) user.getProperty("gender")); 
           } 


          } catch (JSONException e) { 
           Log.e("Facebook parsing data","Error parsing returned user data."); 
          } 

         } else if (response.getError() != null) { 
          if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY) || (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) { 
           Log.e("Facebook parsing data","The facebook session was invalidated."); 
           //onLogoutButtonClicked(); 
          } else { 
           Log.e("Facebook parsing data","Some other error: "+ response.getError().getErrorMessage()); 
          } 
         } 
        } 
       }); 
     request.executeAsync(); 

我已經試過一切,但在以往任何時候我做我收到的電子郵件空:(

回答

0

設置權限如上訪問後email via:user.asMap()。get(「email」));

+0

user.asMap()。獲得( 「電子郵件」))將返回NULL。 –

0

你應該得到的電子郵件這樣

String email = (String) response.getGraphObject().getProperty("email"); 
if(email!=null && !email.equals("") 
//email variable should've email id. 
+0

仍然變爲空,但獲得所有其他字段。無法跟蹤這個錯誤... –

0

嘗試在登錄調用中刪除的權限列表。

ParseFacebookUtils.logIn(SignUpActivity.this, new LogInCallback() { 
//you can add permissions on later screens. 
} 
+0

你能解釋一下你的建議嗎? –

+0

登錄時請勿包含權限。 – nomi13ok

0

請加你這個請求:

Request request = Request.newMeRequest(ParseFacebookUtils.getSession(),new Request.GraphUserCallback() 

在登錄請求條件=> user.isnew()

else if (user.isNew()) { 
       Log.w("FB","User signed up and logged in through Facebook!"); 
       //showUserDetailsActivity(); 
       getProfileInfo(); 
      } 

寫你的區塊,這之間,你就可以也可以通過電子郵件獲取新的用戶信

這完美適用於iOS,肯定會在android中有所幫助。

0

request.executeAsync(); 

添加這三行之前。現在

Bundle parameters = new Bundle(); 
parameters.putString("fields", "id,name,email"); 
request.setParameters(parameters); 

你會得到電子郵件從你的反應

相關問題