安卓:Facebook的获得与QUOT;用户访问令牌",在成功登录令牌、用户、Facebook、QUOT

2023-09-05 06:55:24 作者:少年未老心已凉√

我一直在做通过Facebook最新的SDK 3.0,即一些登录项目。我得到的获取用户访问令牌一个困难时期。我已经在互联网上搜索和所有的,最大的成果是用旧的SDK。这里是code某些位,我已经从 Facebook的Andr​​oid SDK中教程:

I have been doing some login project via Facebook latest SDK i.e. 3.0. I'm getting a hard time in getting user access token. I have searched on the internet and all, maximum results were using the old SDK. Here is some bits of code which I have taken from Facebook Android SDK Tutorial:

public class LoginActivity extends Activity implements OnClickListener {

Button login;
TextView accessToken;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    login = (Button) findViewById(R.id.login);
    accessToken = (TextView) findViewById(R.id.accessToken);

    login.setOnClickListener(this);

}

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

@Override
public void onClick(View v) {
    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {

                // make request to the /me API
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            // callback after Graph API response with user
                            // object
                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {
                                    TextView welcome = (TextView) findViewById(R.id.welcome);
                                    welcome.setText("Hello "
                                            + user.getName() + "!");
                                }
                            }
                        });
            }
        }
    });

}

}

登录成功,我可以看到用户名的应用程序,所建议的Facbeook教程。

Login was successful and I can see Username in the app, as suggested by the Facbeook tutorial.

我都试过老办法,但这些都是pcated现在去$ P $。请指引我在得到用户访问令牌。帮助将AP preciated。

I have tried old methods, but those all are now deprecated. Please guide me in getting User Access Token. Help will be appreciated.

感谢。

推荐答案

在方法onResume()中添加以下code(在这种情况下,我用Toast.makeText看到登录后令牌访问):

In the method onResume() add the following code (in this case i used Toast.makeText to see the token access after login):

    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        Toast.makeText(getActivity(), session.getAccessToken(), Toast.LENGTH_LONG).show();
    }

我用getActivity,因为它是在我的情况下,一个片段,如果你有你的登录按钮,在活动中使用的本,而不是getActivity()

I used getActivity because it is in a Fragment in my case, if you have your login button in an Activity use "this" instead of "getActivity()"

 
精彩推荐
图片推荐