2015-10-15 66 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()); 
     } 
    }); 
相关问题