11

FacebookSDKのログインボタンを使用するアプリケーションを開発しています。私はaccess_tokenユーザーを取得することができますが、私も欲しいですuserID。私はほとんどすべてを試しましたが、成功しませんでした。私はそれを達成するためにFacebookのチュートリアルを使用しました。リンクは次のとおりです。Facebookログインボタン

userID同じログインボタンから取得するのを手伝ってください。

どんな種類の助けもいただければ幸いです。

4

8 に答える 8

11

FB Profile クラス(Facebook SDK 4.0)の getId メソッドを使用します。

  Profile profile = Profile.getCurrentProfile();
  System.out.println(profile.getFirstName());
  System.out.println(profile.getId());
于 2015-04-03T17:21:43.370 に答える
4

ログインに成功したら、次のコードを呼び出す必要があります。応答で id を使用して、ログインしているユーザーの詳細を取得できます。

Session session = Session.getActiveSession();
Request request = Request.newGraphPathRequest(session, "me", null);
com.facebook.Response response = Request.executeAndWait(request)
于 2013-03-22T12:48:22.820 に答える
4

新しい Facebook SDK 3.0 を使用する必要があります。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        if (Session.getActiveSession().isOpened()) {
            // Request user data and show the results
            Request.executeMeRequestAsync(Session.getActiveSession(), new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    // TODO Auto-generated method stub
                    if (user != null) {
                        // Display the parsed user info
                        Log.v(TAG, "Response : " + response);
                        Log.v(TAG, "UserID : " + user.getId());
                        Log.v(TAG, "User FirstName : " + user.getFirstName());

                    }
                }

            });
        }
    }
于 2013-07-01T05:28:29.990 に答える
3

非推奨の API を使用する@Ketan Mehta ( https://stackoverflow.com/a/17397931/624109 )からの回答に基づいて、次のコードを使用する必要があります。

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

    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

    if (Session.getActiveSession().isOpened())
    {
        // Request user data and show the results
        Request.newMeRequest(Session.getActiveSession(), new GraphUserCallback()
        {
            @Override
            public void onCompleted(GraphUser user, Response response)
            {
                if (null != user)
                {
                    // Display the parsed user info
                    Log.v(TAG, "Response : " + response);
                    Log.v(TAG, "UserID : " + user.getId());
                    Log.v(TAG, "User FirstName : " + user.getFirstName());

                }
            }
        }).executeAsync();
    }
}
于 2014-02-14T10:12:38.053 に答える
3

これらのすべての回答は古い SDK に対するものであるため、SDK 4.0 のコードは次のとおりです。

loginButton.registerCallback(callbackManager, new    FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Profile profile = Profile.getCurrentProfile();

            Log.d("facebook",profile.getId());
        }

        @Override
        public void onCancel() {
            // App code
            Log.d("facebook","failed");
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Log.d("facebook","failed");
        }
    });

セッションオブジェクトにアクセスできなくなったように見える同じものを探していたので、上記はユーザープロファイルとユーザーIDを取得する新しい方法です。

于 2015-05-16T10:56:36.633 に答える
2

Facebook グラフ API を使用すると、JSON を返す呼び出しを行うことができます。これは、以下のコードのように JSONObject を使用して Android で解析できます。

JSONObject me = new JSONObject(fb.request("me"));
String id = me.getString("id");
于 2013-03-22T10:42:03.997 に答える
1

次のコードでユーザー ID を取得できます。

document.getElementById('status').innerHTML =
   'Welcome ' + response.name + '!' + "<br />"+
   'Your user ID is : ' + response.id;
于 2015-07-11T11:03:56.007 に答える