0

试图获取 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);
     }
    }
4

3 回答 3

1

请使用 facebook 图形 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) {

        }
于 2014-11-26T06:01:00.093 回答
0

FB 用户对象有一个 email 字段,但由于某种原因 FB 没有提供 get 方法来访问它。只需使用以下行:

    try {
            email.setText(user.getInnerJSONObject().getString("email"));
        }
            catch (JSONException e) { }
于 2015-01-19T18:10:58.083 回答
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.

希望这个答案对你朋友有帮助:)

于 2014-11-26T05:56:37.970 回答