2014-11-26 13 views
0

이름, 위치 및 이메일과 같은 facebook 사용자 세부 정보를 얻으려고합니다. 이름과 위치를 알 수 있지만 이메일이 도착하지 않습니다. 개발자 페이지의 이메일을 확인한 후 승인이 필요합니다. 내 코드에 권한을 추가하는 방법을 얻지 못했습니다. 도와주세요. 감사합니다.android에서 facebook sdk를 사용하여 facebook 사용자의 이메일을받는 방법

public class MainActivity extends ActionBarActivity { 
    // Create, automatically open (if applicable), save, and restore the 
    // Active Session in a way that is similar to Android UI lifecycles. 
    private UiLifecycleHelper uiHelper; 
    private View otherView; 
    private static final String TAG = "MainActivity"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // Set View that should be visible after log-in invisible initially 
     otherView = (View) findViewById(R.id.other_views); 
     otherView.setVisibility(View.GONE); 
     // To maintain FB Login session 
     uiHelper = new UiLifecycleHelper(this, callback); 
     uiHelper.onCreate(savedInstanceState); 
    } 

    // Called when session changes 
    private Session.StatusCallback callback = new Session.StatusCallback() { 
     @Override 
     public void call(Session session, SessionState state, 
       Exception exception) { 
      onSessionStateChange(session, state, exception); 
     } 
    }; 

    // When session is changed, this method is called from callback method 
    private void onSessionStateChange(Session session, SessionState state, 
      Exception exception) { 
     final TextView name = (TextView) findViewById(R.id.name); 
     final TextView gender = (TextView) findViewById(R.id.gender); 
     final TextView location = (TextView) findViewById(R.id.location); 
     final TextView email = (Textview) findViewbyId(R.id.email); 
     // When Session is successfully opened (User logged-in) 
     if (state.isOpened()) { 
      Log.i(TAG, "Logged in..."); 
      // make request to the /me API to get Graph user 
      Request.newMeRequest(session, new Request.GraphUserCallback() { 

       // callback after Graph API response with user 
       // object 
       @Override 
       public void onCompleted(GraphUser user, Response response) { 
        if (user != null) { 
         // Set view visibility to true 
         otherView.setVisibility(View.VISIBLE); 
         // Set User name 
         name.setText("Hello " + user.getName()); 
         // Set Gender 
         gender.setText("Your Gender: " 
           + user.getProperty("gender").toString()); 
         location.setText("Your Current Location: " 
           + user.getLocation().getProperty("name") 
             .toString()); 
    email.setText("Your email: " 
           + user.getProperty("email").toString()); 
        } 
       } 
      }).executeAsync(); 
     } else if (state.isClosed()) { 
      Log.i(TAG, "Logged out..."); 
      otherView.setVisibility(View.GONE); 
     } 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     uiHelper.onActivityResult(requestCode, resultCode, data); 
     Log.i(TAG, "OnActivityResult..."); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     uiHelper.onResume(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     uiHelper.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     uiHelper.onDestroy(); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     uiHelper.onSaveInstanceState(outState); 
    } 
    } 

답변

0

이메일에 대한 추가 권한이 필요하지 않습니다. 다음 자습서를보십시오, 당신은 당신이

http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

여기

final String email = profile.getString("email"); will give you email from the json data. 

희망이 답변이 당신에게 친구 :

도움이된다 로그인 후 이름, 프로필 그림, 이메일, 사용자 ID 등을 얻을 수 있습니다
1

facebook 그래프 API를 사용해주세요. URL : https://graph.facebook.com/me?access_token=session.getAccessToken();

CODE :

try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpGet httppost = new HttpGet("https://graph.facebook.com/me?access_token=" 
        + accessToken); 
      HttpResponse response = httpclient.execute(httppost); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(
        response.getEntity().getContent())); 
      String line = ""; 
      StringBuilder sb = new StringBuilder(); 
      while ((line = rd.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      String result = sb.toString(); 
      JSONObject mJsonObj = new JSONObject(result); 

      mEmailId = mJsonObj.optString("email"); 
      mProfileId = mJsonObj.optString("id"); 

     } catch (Exception ex) { 

     } 
0

FB 사용자 개체가 이메일 필드를 가지고 있지만 어떤 이유로 FB가 액세스 할 수있는 get 메소드를 제공하지 않았다. 다음 줄을 사용하십시오.

try { 
      email.setText(user.getInnerJSONObject().getString("email")); 
     } 
      catch (JSONException e) { }