Facebook Login Through Android
I write an Android application that integrates facebook, but failing in the login to facebook step. What I'm doing basically is to perform authorization and then ask if the session
Solution 1:
Note that authorize method is asynchronous.
You should implement an onComplete method of DialogListener and make all the work you need (such as graph API me request) there.
Solution 2:
Use following code in your app:
publicclassFacebookLogin {
privateAsyncFacebookRunner mAsyncRunner;
privateFacebook facebook;
privateContext mContext;
privateString mFName;
publicstatic final String[] PERMISSIONS = newString[] {"email", "publish_checkins", "publish_stream","offline_access"};
publicFacebookLogin(Context mContext) {
this.mContext=mContext;
facebook=newFacebook(YOUR_APP_ID);
mAsyncRunner = newAsyncFacebookRunner(facebook);
}
publicvoidLogin() {
facebook.authorize((Activity) mContext,PERMISSIONS,Facebook.FORCE_DIALOG_AUTH,newLoginDialogListener());
}
publicvoidLogout() throws MalformedURLException, IOException {
facebook.logout(mContext);
}
publicbooleanisValidUser() {
return facebook.isSessionValid();
}
classLoginDialogListenerimplementsDialogListener {
publicvoidonComplete(Bundle values) {
//Save the access token and access expire for future use in shared prefereceString profile=facebook.request("me")
String uid = profile.getString("id");
mFName= profile.optString("first_name");
newSession(facebook, uid, mFName).save(mContext);
}
publicvoidonFacebookError(FacebookError error) {
displayMessage("Opps..! Check for Internet Connection, Authentication with Facebook failed.");
}
publicvoidonError(DialogError error) {
displayMessage("Opps..! Check for Internet Connection, Authentication with Facebook failed.");
}
publicvoidonCancel() {
displayMessage("Authentication with Facebook failed due to Login cancel.");
}
}
}
On Login complete save the facebook access token and access expire in your shared preference and while using again facebook object later set that access token and access expire to facebook object , it will not give the error which occurs in your code.
you can use following class :
publicclassSession {
privatestaticSession singleton;
privatestaticFacebook fbLoggingIn;
// The Facebook objectprivateFacebook fb;
// The user id of the logged in userprivateString uid;
// The user name of the logged in userprivateString name;
/**
* Constructor
*
* @paramfb
* @paramuid
* @paramname
*/publicSession(Facebook fb, String uid, String name) {
this.fb = fb;
this.uid = uid;
this.name = name;
}
/**
* Returns the Facebook object
*/publicFacebookgetFb() {
return fb;
}
/**
* Returns the session user's id
*/publicStringgetUid() {
return uid;
}
/**
* Returns the session user's name
*/publicStringgetName() {
return name;
}
/**
* Stores the session data on disk.
*
* @paramcontext
* @return
*/publicbooleansave(Context context) {
Editor editor =
context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE).edit();
editor.putString(ConstantsFacebook.TOKEN, fb.getAccessToken());
editor.putLong(ConstantsFacebook.EXPIRES, fb.getAccessExpires());
editor.putString(ConstantsFacebook.UID, uid);
editor.putString(ConstantsFacebook.NAME, name);
editor.putString(ConstantsFacebook.APP_ID, fb.getAppId());
editor.putBoolean(ConstantsFacebook.LOGIN_FLAG,true);
if (editor.commit()) {
singleton = this;
returntrue;
}
returnfalse;
}
/**
* Loads the session data from disk.
*
* @paramcontext
* @return
*/publicstaticSessionrestore(Context context) {
if (singleton != null) {
if (singleton.getFb().isSessionValid()) {
return singleton;
} else {
returnnull;
}
}
SharedPreferences prefs =
context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE);
String appId = prefs.getString(ConstantsFacebook.APP_ID, null);
if (appId == null) {
returnnull;
}
Facebook fb = newFacebook(appId);
fb.setAccessToken(prefs.getString(ConstantsFacebook.TOKEN, null));
fb.setAccessExpires(prefs.getLong(ConstantsFacebook.EXPIRES, 0));
String uid = prefs.getString(ConstantsFacebook.UID, null);
String name = prefs.getString(ConstantsFacebook.NAME, null);
if (!fb.isSessionValid() || uid == null || name == null) {
returnnull;
}
Session session = newSession(fb, uid, name);
singleton = session;
return session;
}
/**
* Clears the saved session data.
*
* @paramcontext
*/publicstaticvoidclearSavedSession(Context context) {
Editor editor =
context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
singleton = null;
}
/**
* Freezes a Facebook object while it's waiting for an auth callback.
*/publicstaticvoidwaitForAuthCallback(Facebook fb) {
fbLoggingIn = fb;
}
/**
* Returns a Facebook object that's been waiting for an auth callback.
*/publicstaticFacebookwakeupForAuthCallback() {
Facebook fb = fbLoggingIn;
fbLoggingIn = null;
return fb;
}
publicstaticStringgetUserFristName(Context context) {
SharedPreferences prefs =
context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE);
String frist_name = prefs.getString(ConstantsFacebook.NAME, null);
return frist_name;
}
publicstaticbooleancheckValidSession(Context context) {
SharedPreferences prefs =
context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE);
Boolean login=prefs.getBoolean(ConstantsFacebook.LOGIN_FLAG,false);
return login;
}
}
Post a Comment for "Facebook Login Through Android"