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.
Code below is updated:
I'm new in Android ..
I'm using the above code for getting the token only.
In the method onResume() add the following code (in this case i used Toast.makeText to see the token access after login):
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()"
I successfully managed to integrate the Facebook sdk in my my with the help of following code:
I hope, you will get some help with this.
To get the accessToken for facebook-sdk 4.* or above. Add this lines after facebookSDKInitialize()
After that use these lines of codes,
Sample:
Note: Session class is removed in new Facebook sdk
After
Facebook SDK version 4.X
, you should use following:Change log reference is here.
Migrate reference from Sdk 3.x to 4.x