简体   繁体   English

使用android SDK 4.0获取Facebook用户ID

[英]Get Facebook user ID with android SDK 4.0

How can I get the Facebook User ID with the Facebook Android SDK 4.0? 如何使用Facebook Android SDK 4.0获取Facebook用户ID?

Here is my code that doesn't work: 这是我的代码不起作用:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_firstlogin);

    CallbackManager cm;
    LoginButton     lb;

    cm = CallbackManager.Factory.create();
    lb = (LoginButton) findViewById(R.id.facebook_button);
    Log.d(TAG, "Salut les zouzous");
    LoginManager.getInstance().registerCallback(cm,
            new FacebookCallback<LoginResult>()
            {
                @Override
                public void onSuccess(LoginResult loginResult)
                {
                   Log.d(TAG, "Success !");
                    AccessToken tok;
                    tok = AccessToken.getCurrentAccessToken();
                    Log.d(TAG, tok.getUserId());
                }

                @Override
                public void onCancel()
                {
                    Log.d(TAG, "On Cancel");
                }

                @Override
                public void onError(FacebookException exception)
                {
                    Log.e(TAG, exception.getMessage());
                }
            });

I precise that the 我精确地说

 Log.d(TAG, " Success"); 

doesn't print. 不打印。

There is only the first log who print something, outside the callback. 在回调之外,只有第一个打印内容的日志。

Sorry for the bad English. 对不起,英语不好。

As simple as 就像

Profile.getCurrentProfile().getId()

This assumes the user is already logged in. 假设用户已经登录。

You will need to do the following 您将需要执行以下操作

 @Override
        public void onSuccess(LoginResult loginResult) {
            final AccessToken accessToken = loginResult.getAccessToken();

            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                    Log.d(TAG, user.optString("email"));
                    Log.d(TAG, user.optString("name"));
                    Log.d(TAG, user.optString("id"));
                }
            }).executeAsync();
        }

just use loginResult.getAccessToken().getUserId() in onSuccess() as 只需在onSuccess()中使用loginResult.getAccessToken().getUserId() ()作为

@Override
public void onSuccess(LoginResult loginResult) {

    Toast.makeText(Login.this, "User ID : "+loginResult.getAccessToken().getUserId(), Toast.LENGTH_LONG).show();

}

If you're using the Login Button that is provided by Facebook then I think instead of 如果您使用的是Facebook提供的“登录按钮”,那么我认为不是

LoginManager.getInstance().registerCallback()

you need to use 你需要使用

lb.registerCallback()

That could explain why your callback isn't firing at all. 那可以解释为什么您的回调根本不触发。

****edit**** ****编辑****

If your callback's still aren't firing and you only need the UserID you could go for AccessToken Tracking instead. 如果您的回调仍未触发,而您仅需要UserID,则可以使用AccessToken Tracking。

Facebook offers an example where you override the onActivityResult method of the Activity/Fragment that you've declared the button in: Facebook提供了一个示例,其中您覆盖在其中声明了按钮的Activity / Fragment的onActivityResult方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM