2015-10-15 86 views
-1

這是我在Facebook登錄後獲取用戶信息的代碼。 我想從用戶獲取emailid我得到的名稱,id,但沒有得到emailid。我已經嘗試登錄按鈕和登錄管理器類都給出了相同的結果。 現在如何從回覆中獲得電子郵件ID:如何獲取電子郵件ID從Android Facebook SDK 4.6.0?

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
       @Override 
       public void onSuccess(final LoginResult loginResult) { 
        new GraphRequest(AccessToken.getCurrentAccessToken(), 
          "/me", null , HttpMethod.GET, 
          new GraphRequest.Callback() { 
           public void onCompleted(
             GraphResponse response) { 
             handle the result 

            if (response !=null ) 
            { 
             //GET USER INFORMATION 
             Toast.makeText(getApplicationContext(),"Hello in JSON",Toast.LENGTH_LONG).show(); 
             JSONObject json = response.getJSONObject(); 
             //JSONArray jsona = response.getJSONArray(); 
             Log.i(TAG,"" +json); 
             // Log.i(TAG,"" +json); 
             String email = json.optString("email"); 
             // String email = json.optString("email"); 
             //Log.i("email", "" + email); 
             Log.i(TAG,"" +email); 
             String fullName = json.optString("name"); 
             String location = json.optString("location"); 
             String accessToken = loginResult.getAccessToken().getToken(); 
             String user_id = json.optString("id"); 
             Log.i(TAG,"" +json); 
             Toast.makeText(getApplicationContext(),fullName,Toast.LENGTH_LONG).show(); 
             Toast.makeText(getApplicationContext(),email,Toast.LENGTH_LONG).show(); 
             Toast.makeText(getApplicationContext(),user_id,Toast.LENGTH_LONG).show(); 
             //int type = 1; 
             // String lastUpdate = json.getString("updated_time"); 

             // Log.i("email", "" + email); 
             // Log.i("Message", "HELLO"); 
             Log.i("Name", "" + fullName); 
             Log.i("ID", "" + user_id); 


            } 
           } 

          }).executeAsync(); 

謝謝.. !!

+1

威爾你是誰ase添加更多的信息關於'你在哪裏卡住了?' – AndiGeeky

+0

@AndiGeeky嗨我使用的是Facebook sdk 4.6.0和API版本2.5我在上面的代碼中我得到的名稱,id但沒有得到使用json對象的電子郵件ID。請任何消化不良? – Sidh

+0

調試你的應用程序,並檢查'​​GraphResponse響應'中的內容。 – AndiGeeky

回答

1
LoginManager.getInstance().logInWithReadPermissions(WelcomeActivity1.this, (Arrays.asList("public_profile", "user_friends","user_birthday","user_about_me","email"))); 

String email; 


LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() { 
@Override 
    public void onSuccess(LoginResult loginResult) { 
    Log.d("tag","FF fb onSuccess"); 
    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() { 
    @Override 
    public void onCompleted(JSONObject object,GraphResponse response) { 
    try { 
      String[] splited ; 
      JSONObject obj = object.getJSONObject("picture").getJSONObject("data"); 

           if (object.has("email")) 
           { 
           email = object.getString("email"); 
           } 
           else 
           { 
            email = ""; 
           } 



          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 


          } 
         }); 
       Bundle parameters = new Bundle(); 
       parameters.putString("fields", "id,name,link,birthday,picture,email,gender"); 
       request.setParameters(parameters); 
       request.executeAsync(); 


      } 

      @Override 
      public void onCancel() { 
       Log.d("tag","fb onCancel"); 
       // App code 
      } 



@Override 
      public void onError(FacebookException exception) { 
       Log.d("tag","fb onError"); 
       // App code 
      } 
}); 
+0

使用上面的代碼,從Facebook帳戶中檢索電子郵件ID – Rotomac17

+0

您的代碼適合我。感謝帖子.. !!但如何? Bundle parameters = new Bundle(); 參數。putString(「fields」,「id,name,link,birthday,picture,email,gender」); request.setParameters(參數); request.executeAsync();它是如何執行的,因爲在這裏我需要將它傳遞給其他變量? – Sidh

+0

我可以知道你需要傳遞給其他變量的值嗎? – Rotomac17

0

從Graph API v2.4開始,您需要指定您希望在查詢中返回的每個字段。

new GraphRequest(AccessToken.getCurrentAccessToken(), 
    "/me", null , HttpMethod.GET, 
    new GraphRequest.Callback() { 
    ... 

需要成爲

new GraphRequest(AccessToken.getCurrentAccessToken(), 
    "/me?fields=id,name,email", null , HttpMethod.GET, 
    new GraphRequest.Callback() { 
    ... 

這是非常有據可查,並有數十家在這裏如此相似的問題。發佈問題前請先參閱文檔。

請記住,您需要適當的權限才能請求用戶詳細信息。在這種情況下,它是email

參見:

+0

。我用你的代碼替換代碼,但其仍然相同的輸出即將到來沒有改變是有其他東西我需要改變.. ?? – Sidh

+0

你有沒有'email'權限? – Tobi

+0

我已在登錄管理器中獲得上述許可。是的,我獲得了電子郵件許可.. !!!! email返回null .. LoginManager.getInstance()。logInWithReadPermissions(facebooklogin.this,Arrays.asList(「email」)); – Sidh

1

這種方式獲得的Facebook登錄的一部分...

locationMangaer = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    loginButton = (LoginButton)findViewById(R.id.login_button); 
    loginButton.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile", "user_friends")); 

    callbackManager= CallbackManager.Factory.create(); 
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      GraphRequest request = GraphRequest.newMeRequest(
        loginResult.getAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback() { 
         @Override 
         public void onCompleted(
           JSONObject object, 
           GraphResponse response) { 
          // Application code 
          Log.e("LoginActivity", response.toString() + " " + object.toString()); 

          try { 
           String name=object.getString("name"); 
           String email=object.getString("email"); 
           String id=object.getString("id"); 
           String gender=object.getString("gender"); 




           Log.e("LoginActivity", name + " " + email + " " + id + " " + gender); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         } 
        }); 



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

     } 

     @Override 
     public void onCancel() { 
      // App code 
      Log.e("LOGIN Cancle", "LOGIN Cancle"); 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      Log.e("LOGIN Cancle", exception.getMessage()); 
     } 
    }); 
相關問題