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() + "!");
}
}
});
}
}
});
}
}
Login was successful and I can see Username in the app, as suggested by the Facbeook tutorial.
I have tried old methods, but those all are now deprecated. Please guide me in getting User Access Token. Help will be appreciated.
Thanks.
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();
}
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()"