簡體   English   中英

通過Facebook成功登錄后如何獲取用戶詳細信息

[英]How to get user details after successful Login through Facebook

我試過這個:當isSessionValid getDetails直接其他,facebook.authorize,然后onActivityResult中的getDetails

public class MainActivity extends Activity {

    Facebook facebook = new Facebook("xxxxxxxxxxxxxxxx");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    private SharedPreferences mPrefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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[] {}, new DialogListener() {
                @Override
                public void onComplete(Bundle values) {
                    SharedPreferences.Editor editor = mPrefs.edit();
                    editor.putString("access_token", facebook.getAccessToken());
                    editor.putLong("access_expires",
                            facebook.getAccessExpires());
                    editor.commit();
                }

                @Override
                public void onFacebookError(FacebookError error) {
                }

                @Override
                public void onError(DialogError e) {
                }

                @Override
                public void onCancel() {
                }
            });
        }else{

            try {
                JSONObject json = Util.parseJson(facebook.request("me"));
                String facebookID = json.getString("id");
                String firstName = json.getString("first_name");
                String lastName = json.getString("last_name");
                String email = json.getString("email");
                String gender = json.getString("gender");
            } catch (Exception e) {

            }
        }
    }

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

        facebook.authorizeCallback(requestCode, resultCode, data);
        try {
            JSONObject json = Util.parseJson(facebook.request("me"));
            String facebookID = json.getString("id");
            String firstName = json.getString("first_name");
            String lastName = json.getString("last_name");
            String email = json.getString("email");
            String gender = json.getString("gender");

        } catch (Exception e) {

        }
    }

    public void onResume() {
        super.onResume();
        facebook.extendAccessTokenIfNeeded(this, null);
    }
}

當我的系統上安裝了Facebook應用程序時,此方法工作正常。 但是,如果未安裝,我將獲得一個Web視圖以輸入facebook憑據,並在logcat中顯示登錄成功,但是沒有調用getDetails塊。

在這里,您可以通過initFacebook()函數登錄並執行您的功能,在這里,我正在獲取用戶的朋友信息。

private void initFacebook() 
{
    try
    {
        if (APP_ID == null) 
        {
            Util.showAlert(this,"Warning","Facebook Applicaton ID must be "+ "specified before running this example: see Example.java");
        }

        mFacebook = new Facebook();
        mAsyncRunner = new AsyncFacebookRunner(mFacebook);
        mFacebook.authorize(FacebookList.this, APP_ID, new String[] {"email", "read_stream", "user_hometown", "user_location","friends_about_me", "friends_hometown", "friends_location","user_relationships", "friends_relationship_details","friends_birthday", "friends_education_history","friends_website" }, new DialogListener()
        {
            public void onComplete(Bundle values) 
            {
                getHTTPConnection();
            }

            public void onFacebookError(FacebookError error) 
            {
                Log.i("public void onFacebookError(FacebookError error)....","....");
            }

            public void onError(DialogError e) 
            {
                Log.i("public void onError(DialogError e)....", "....");
                CustomConfirmOkDialog dialog = new CustomConfirmOkDialog(FacebookList.this, R.style.CustomDialogTheme, Utils.FACEBOOK_CONNECTION_ERROR);
                dialog.show();
            }

            public void onCancel() 
            {
                Log.i("public void onCancel()....", "....");
            }
        });

        SessionStore.restore(mFacebook, this);
        SessionEvents.addAuthListener(new SampleAuthListener());
        SessionEvents.addLogoutListener(new SampleLogoutListener());
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

在這里,在getHTTPConnection() ,進行連接和發送字段,這是我們需要的關於用戶朋友的條件,因為在這里,我們可以看到傳遞的字段是fields=id,first_name,last_name,location,picture friends fields=id,first_name,last_name,location,picture 在這里,您可以根據應用程序的要求更改此字段。

private void getHTTPConnection() 
{
    try
    {
        mAccessToken = mFacebook.getAccessToken();
        HttpClient httpclient = new DefaultHttpClient();
        String result = null;
        HttpGet httpget = new HttpGet("https://graph.facebook.com/me/friends?access_token="+ mAccessToken + "&fields=id,first_name,last_name,location,picture");
        HttpResponse response;
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) 
        {
            result = EntityUtils.toString(entity);
            parseJSON(result);
        }
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

現在,與Facebook成功連接后,我們將獲取JSON數據並進一步對其進行解析。

private void parseJSON(String data1) throws Exception,NullPointerException, JSONException 
{
    try 
    {
        JSONObject jObj = new JSONObject(data1);
        JSONArray jObjArr = jObj.optJSONArray("data");
        int lon = jObjArr.length();


        for (int i = 0; i < lon; i++) 
        {
            JSONObject tmp = jObjArr.optJSONObject(i);

            String temp_image =  tmp.getString("picture");                          String temp_fname = tmp.getString("first_name");

            String temp_lname = tmp.getString("last_name");

            String temp_loc = null;

            JSONObject loc = tmp.getJSONObject("location");
            temp_loc = loc.getString("name");


        }
    } 
    catch (Exception e) 
    {
        Log.i("Exception1 is Here>> ", e.toString());
        e.printStackTrace();
    }
}

假設您已經在應用程序中添加了一個Facebook jar,並且可以執行以下代碼將initFacebook()調用到活動的onCreate()

暫無
暫無

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

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