簡體   English   中英

如何在Android中使用Facebook SDK獲取Facebook用戶的電子郵件

[英]How to get email of facebook user using facebook sdk in android

試圖獲取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);
     }
    }

請使用facebook graph API。 網址: https : //graph.facebook.com/me?access_token=session.getAccessToken() ;

碼:

   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) {

        }

您不需要任何額外的電子郵件許可。 請嘗試以下教程,登錄后即可獲取名稱,個人資料圖片,電子郵件,用戶ID等

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

這里

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

希望這個答案對您的朋友有幫助:)

FB用戶對象具有一個電子郵件字段,但是由於某些原因,FB沒有提供訪問它的get方法。 只需使用以下行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM