如何从Facebook用户获取用户ID?

时间:2012-09-12 10:38:30

标签: android facebook sdk

我想从Facebook用户那里获取id,但到目前为止我尝试的内容都不起作用......

public class fbLogin extends Activity {

    public static final int LOGIN = Menu.FIRST;
    public static final int GET_EVENTS = Menu.FIRST + 1;
    public static final int GET_ID = Menu.FIRST + 2;

    public static final String APP_ID = "361579407254212";
    public static final String TAG = "FACEBOOK CONNECT";

    private Facebook mFacebook;
    private AsyncFacebookRunner mAsyncRunner;
    private Handler mHandler = new Handler();

    private static final String[] PERMS = new String[] { "user_events","email" };

    private TextView mText;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.onCreate(savedInstanceState);

            // setup the content view
            initLayout();

            // setup the facebook session
            mFacebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(mFacebook);

            if (mFacebook.isSessionValid()) {
                AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook);
                asyncRunner.logout(this, new LogoutRequestListener());
            } else {
                mFacebook.authorize(this, PERMS, new LoginDialogListener());
            }

            mAsyncRunner.request("me", new IDRequestListener());    
    }

    protected void initLayout() {
        LinearLayout rootView = new LinearLayout(this.getApplicationContext());
        rootView.setOrientation(LinearLayout.VERTICAL);

        this.mText = new TextView(this.getApplicationContext());
        this.mText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT));
        rootView.addView(this.mText);

        this.setContentView(rootView);

    }


    private class LoginDialogListener implements DialogListener {

        public void onComplete(Bundle values) {}    

        public void onFacebookError(FacebookError e) {}

        public void onError(DialogError e) {}

        public void onCancel() {}
    }

    private class LogoutRequestListener implements RequestListener {

        public void onComplete(String response, Object state) {
            // Dispatch on its own thread
            mHandler.post(new Runnable() {
                public void run() {
                    mText.setText("Logged out");
                }
            });
        }

        public void onIOException(IOException e, Object state) {}

        public void onFileNotFoundException(FileNotFoundException e, Object state) {}

        public void onMalformedURLException(MalformedURLException e, Object state) {}

        public void onFacebookError(FacebookError e, Object state) {}
    }

    private class IDRequestListener implements RequestListener {

        public void onComplete(String response, Object state) {
            try {
                // process the response here: executed in background thread
                Log.d(TAG, "Response: " + response.toString());
                JSONObject json = Util.parseJson(response);
                final String id = json.getString("id");

                fbLogin.this.runOnUiThread(new Runnable() {
                    public void run() {
                        mText.setText("Hello there, " + id + "!");
                    }
                });
            } catch (JSONException e) {
                Log.w(TAG, "JSON Error in response");
                mText.setText("catch1...");
            } catch (FacebookError e) {
                mText.setText("catch2");
            }
        }

        public void onIOException(IOException e, Object state) {mText.setText("Logging out...1");}

        public void onFileNotFoundException(FileNotFoundException e, Object state) {mText.setText("Logging out...2");}

        public void onMalformedURLException(MalformedURLException e,Object state) {mText.setText("Logging out...3");}

        public void onFacebookError(FacebookError e, Object state) {mText.setText("Logging out...4");}
    }
}

当我从OnComplete类转到IDRequestListener方法时,它会出现Facebook错误...

我该如何解决?有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

你可以使用socialauth android sdk。可以获取用户个人资料,发布消息,获取朋友列表

http://code.google.com/p/socialauth-android/

答案 1 :(得分:0)

authorize()不同步,会立即返回。在提交需要访问令牌的请求之前,您需要等待授权请求完成。基本上,只有在me监听器中调用onComplete()时才会触发authorize()请求。

此外,您的活动似乎缺少为onActivityResult()对象提供授权结果的Facebook实施,即

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