Pages

Thursday 24 January 2013

android facebook login implementation

use below code for facebook login.

----- FOR NEW SDK-----

https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

-----FOR OLD SDK------
you need to use facebook sdk. you can download from developer.facebook.com


private void facebookLogin() {

  facebook = new Facebook(General.FB_APPID);// ((GlobalVars)getApplicationContext()).facebook;

  String access_token = prefs.getString(General.PREFS_FB_token, null);
  Long expires = prefs.getLong(General.PREFS_FB_expires, 0);
  Log.d("MyTag", "token:" + access_token);
  if (access_token != null) {
   facebook.setAccessToken(access_token);
  }
  if (expires != 0) {
   facebook.setAccessExpires(expires);
  }
  /*
   * Only call authorize if the access_token has expired.
   */

  if (!facebook.isSessionValid()) {
   final Editor edit = prefs.edit();
   Log.d("MyTag", "In Authorize");
   facebook.authorize(this,
     new String[] { "publish_stream", "email" },
      new DialogListener() {
      public void onComplete(Bundle values) {
       Log.d("fb login complete",
         "fb login complete");
       Log.d("MyTag",
         "face token: " + facebook.getAccessToken());
       edit.putString(General.PREFS_FB_token,
         facebook.getAccessToken());
       
       edit.putLong(General.PREFS_FB_expires,
         facebook.getAccessExpires());
       edit.commit();
       Toast.makeText(SettingsActivity.this, "login success", Toast.LENGTH_LONG).show();
      }

      public void onFacebookError(FacebookError e) {
       Toast.makeText(getApplicationContext(),
         "onFacebookError", Toast.LENGTH_SHORT)
         .show();
      }

      public void onError(DialogError e) {
       Toast.makeText(getApplicationContext(), "onError",
         Toast.LENGTH_SHORT).show();
      }

      public void onCancel() {

      }
     });
  } else {
   Toast.makeText(this, "login success", Toast.LENGTH_LONG).show();
  }
 }

No comments :

Post a Comment