0
private void loginToFacebook() {
    // TODO Auto-generated method stub
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());

                        try {
                            // PROFILE DETAILS AND FRIENDS COUNTER
                            String queryUserInfo = "SELECT uid FROM user WHERE uid = me()";

                            Bundle paramUserInfo = new Bundle();
                            paramUserInfo.putString("method", "fql.query");
                            paramUserInfo.putString("query", queryUserInfo);

                            String responseUserInfo = Util.facebook
                                    .request(paramUserInfo);

                            JSONArray JAUserData = new JSONArray(
                                    responseUserInfo);

                            for (int i = 0; i < JAUserData.length(); i++) {
                                JSONObject JOUserData = JAUserData
                                        .getJSONObject(i);

                                if (JOUserData.has("uid")) {
                                    String getUserID = JOUserData
                                            .getString("uid");
                                }

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        /*
                         * try { editor.putString("access_expires",
                         * facebook.request("me"));
                         * 
                         * try { JSONObject json =
                         * Util.parseJson(facebook.request("me", values));
                         * String userId = json.getString("id");
                         * 
                         * } catch (FacebookError e) { // TODO
                         * Auto-generated catch block e.printStackTrace(); }
                         * catch (JSONException e) { // TODO Auto-generated
                         * catch block e.printStackTrace(); }
                         * 
                         * Log.i(TAG, facebook.request("me")); } catch
                         * (MalformedURLException e) { // TODO
                         * Auto-generated catch block e.printStackTrace(); }
                         * catch (IOException e) { // TODO Auto-generated
                         * catch block e.printStackTrace(); }
                         */
                        editor.commit();

                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error

                        Log.wtf(TAG, error);
                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
                        Log.wtf(TAG, fberror);
                    }

                });
    }
}

在我的应用程序登录屏幕中,如果用户单击它,则存在“使用 Facebook 登录”按钮,应用程序在输入用户详细信息后显示 facebook 登录屏幕,这里如何获取 ID。

4

1 回答 1

1

用户登录后,使用以下代码:

try {
    // PROFILE DETAILS AND FRIENDS COUNTER
    String queryUserInfo = "SELECT uid FROM user WHERE uid = me()";

    Bundle paramUserInfo = new Bundle();
    paramUserInfo.putString("method", "fql.query");
    paramUserInfo.putString("query", queryUserInfo);

    String responseUserInfo = Utility.mFacebook.request(paramUserInfo);

    JSONArray JAUserData = new JSONArray(responseUserInfo);

    for (int i = 0; i < JAUserData.length(); i++) {
        JSONObject JOUserData = JAUserData.getJSONObject(i);

        if (JOUserData.has("uid")) {
            String getUserID = JOUserData.getString("uid");
        }

    }

} catch (Exception e) {
    e.printStackTrace();
}

这将为您提供登录用户的 ID,并将被转换为String getUserID. 我在用户成功登录后的第一个活动中运行这段代码(无论是否使用了Facebook Single Sign On移动登录

只要用户登录,该代码仍然有效,并且可以在任何活动中运行。另外,我通常在Asynctask.

于 2012-09-21T11:41:07.930 回答