2015-11-04 155 views
0

我正在使用Facebook registerCallBack方法获取当前用户信息。我遇到的问题是在尝试检索电子邮件地址时。Facebook Android登录获取用户电子邮件地址

// Callback registration 
    FBLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      final Profile profile = Profile.getCurrentProfile(); 
      if (profile != null) { 

      } 
      //Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show(); 
      GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback() { 
         @Override 
         public void onCompleted(JSONObject object, GraphResponse response) { 
          try { 
           String email_id = object.getString("email"); 
           String gender = object.getString("gender"); 
           String facebook_id=profile.getId(); 
           String f_name=profile.getFirstName(); 
           String m_name=profile.getMiddleName(); 
           String l_name=profile.getLastName(); 
           String full_name=profile.getName(); 
           String profile_image=profile.getProfilePictureUri(400, 400).toString(); 

           Log.i("facebook_id",facebook_id); 
           Log.i("f_name",f_name); 
           Log.i("m_name",m_name); 
           Log.i("l_name",l_name); 
           Log.i("full_name",full_name); 
           Log.i("profile_image",profile_image); 
           Log.i("gender",gender); 
           Log.i("email_id",email_id+""); 

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

         } 

        }); 

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

     @Override 
     public void onCancel() { 
      // App code 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
     } 
    }); 

使用此代码我碰到下面的日志:

org.json.JSONException: No value for email 

我已经确定该电子邮件是公开的,这是一个问题,因为我曾与谷歌加登录类似的问题的情况下脚本。

+0

您需要询问电子邮件字段 – WizKid

+0

@wizkid我在参数中指定电子邮件地址是 – jampez77

+1

哦,您是否要求用户提供电子邮件许可? – WizKid

回答

1

此基础上我的意见,当你得到型动物领域:

parameters.putString("fields", "id, name, email, gender, birthday, friends, albums"); 

你肯定忘了正确的权限。事实上,你可能设置"user_profile"如下:

loginButton.setReadPermissions("user_profile"); 

但是,当你看the reference documentation,你可以看到,"email"是在自己的权限,它不包括在"user_profile"

因此,当您启动LoginManager(参见Add LoginButton section)时,您必须设置不同的权限,包括"email"。相反,只得到一个权限,您可以设置一个数组:

loginButton = (LoginButton) view.findViewById(R.id.login_button); 
loginButton.setReadPermissions(Arrays.asList("user_profile", "email", "...", "...")); 

多亏了这一点,你就可以检索电子邮件地址。

相关问题