Facebook authorization access token? - android

I'm using the tutorial code...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class FacebookSSO extends Activity {
Facebook facebook = new Facebook("ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
I'm wondering... once the user has authenticated how would I store the access token? couldn't see any mention of it on the tutorial.

facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
String token=facebook.getAccessToken(); //get access token
save(token);
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
private void save(String token){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
i.e save it in sharedpreferences

FYI. facebook.authorize had deprecated since Facebook SDK 3

Try in this way
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}

Related

Android:Cannot logout from facebook and pass status

Problem1:When i click login button i can log in,give status.but when i press logout button it does not logout.So,how will i logout from facebook?
Problem2:I got a string str.I want to pass it in facebook's textfield (like we send messages by intent).How will i do that.
main class:
`
public class MainActivity extends Activity {
private static String APP_ID = "xxxxxxx";
private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
ImageButton btnFbLogin;
ImageButton btnFbLogout;
Button btnPostToWall;
String str="I want this to pass";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFbLogin = (ImageButton) findViewById(R.id.btn_fblogin);
btnFbLogout = (ImageButton) findViewById(R.id.btn_fbLogout);
btnPostToWall = (Button) findViewById(R.id.btn_fb_post_to_wall);
mAsyncRunner = new AsyncFacebookRunner(facebook);
btnFbLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
loginToFacebook();
}
});
btnFbLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
logoutFromFacebook();
}
});
btnPostToWall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postToWall();
}
});
}
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
btnFbLogin.setVisibility(View.INVISIBLE);
btnFbLogout.setVisibility(View.VISIBLE);
btnPostToWall.setVisibility(View.VISIBLE);
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
}
#Override
public void onComplete(Bundle values) {
acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
btnFbLogin.setVisibility(View.INVISIBLE);
btnFbLogout.setVisibility(View.VISIBLE);
btnPostToWall.setVisibility(View.VISIBLE);
}
#Override
public void onError(DialogError error) {
}
#Override
public void onFacebookError(FacebookError fberror) {
}
});
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void postToWall() {
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
btnFbLogin.setVisibility(View.VISIBLE);
btnFbLogout.setVisibility(View.INVISIBLE);
btnPostToWall.setVisibility(View.INVISIBLE);
}
});
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
}`
when i click the logout button it says:
`
08-14 14:46:59.849: D/Image Button(284): button Clicked
08-14 14:46:59.869: D/Facebook-Util(284): GET URL: https://api.facebook.com/restserver.php? method=auth.expireSession&format=json
08-14 14:47:00.399: D/Logout from Facebook(284): {"error_code":101,"error_msg":"Invalid application ID.","request_args":[{"key":"method","value":"auth.expireSession"},{"key":"format","value":"json"}]}
`
just clear sharedprefrence and clear cookig to logout from facebook
btnFbLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Image Button", "button Clicked");
Util.clearCookies(youractivity.this);
// your sharedPrefrence
Editor editor = context.getSharedPreferences("MYFB_TOKEN", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
}
});
OR
See this How can i logout from facebook when i click logout button
use this it is working for me
facebook.logout(this);
in Facebook SDK Facebook.java
public String logout(Context context) throws MalformedURLException, IOException {
Util.clearCookies(context);
Bundle b = new Bundle();
b.putString("method", "auth.expireSession");
String response = request(b);
setAccessToken(null);
setAccessExpires(0);
return response;
}

Opening Facebook login Dialog on Button Click

Hi Everyone I am intergrating facebook login to my app. I have searched a lot and I am able to login I have used this link: https://developers.facebook.com/docs/mobile/android/build/#register
All I want to do is that When I click on a Button then facebook dialog must appear intead of just opening the activity (HomeActivity) following is my code.
public class HomeActivity extends Activity {
Facebook facebook = new Facebook("114987225319269");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}
Use this code
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (! facebook.isSessionValid()) {
facebook.authorize(HomeActivity.this, PERMISSIONS, new LoginDialogListener());
});
}
this is the login dialog listener
private class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
getAlbumsData task = new getAlbumsData();
task.execute();
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
In your Manifest
<activity
android:theme="#android:style/Theme.Translucent"
android:name=".HomeActivity "
>
This will change your activity theme into Dialog theme. Hope this helps...
This is all what you need:
public class BaseActivity extends Activity
{
public AsyncFacebookRunner mAsyncRunner;
public Facebook facebook;
public SharedPreferences mPrefs;
public static String appId = "123344";
SharedPreferences.Editor editor;
private void initialize()
{
facebook = new Facebook(appId);
mAsyncRunner = new AsyncFacebookRunner(facebook);
}
public void verifyLogin()
{
mPrefs = getPreferences(MODE_PRIVATE);
String accessToken = mPrefs.getString("access_token", null);
facebook.setAccessToken(accessToken);
long expires = mPrefs.getLong("access_expires", 0);
if (accessToken != null)
{
facebook.setAccessToken(accessToken);
}
if (expires != 0)
{
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid())
{
facebook.authorize(this, new String[] {
"email", "publish_stream", "create_event"
}, new DialogListener()
{
#Override
public void onCancel()
{
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values)
{
// Function to handle complete event
// Edit Preferences and update facebook acess_token
editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error)
{
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror)
{
// Function to handle Facebook errors
}
});
}
}
}

Facebook api not working in my emulator?

I have implemented login with facebook in my project.
The code is below:
public class login extends Activity{
ImageView fbtn;
private SharedPreferences mPrefs;
static Facebook facebook = new Facebook("271089732997803");
String access_token;
long expires;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yf_login1);
mPrefs = getPreferences(MODE_PRIVATE);
access_token = mPrefs.getString("access_token", null);
expires = mPrefs.getLong("access_expires", 0);
fbtn = (ImageView)findViewById(R.id.fbtn);
fbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(access_token != null) {
facebook.setAccessToken(access_token);
Log.v("access_token", access_token);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
Log.i("expires", ""+expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(login.this,new String[] {}, new DialogListener() {
#Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
#Override
public void onFacebookError(FacebookError error) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onCancel() {
}
});
}
else{
startActivity(new Intent(login.this,ChooseTeam.class));
}
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
#Override
public void onResume() {
super.onResume();
facebook.extendAccessTokenIfNeeded(this, null);
}
Now my question is when i click on the the fb button it will load the progress bar and after 30-40 second it returns the same page.
The most important thing is when i run the same project in my another pc it will run perfectly, opens the dialog of login for the facebook and got the value in my preferences. But in my case i cannot even get the preferences made in my data.
What will be the problem in my emulator?? I am using eclipse galilio and sdk20.
I got the solution actually my antivirus is blocked the internet for the emulator. So i have uninstalled the antivirus and run the app and it works.
Thank you all for helping me out.
Try this....
and use this Permission for Share in Facebook..
private String[] mPermissions={"publish_stream"};
mFb.authorize(SettingActivity.this, mPermissions,new com.fbintegration.Facebook.DialogListener()
{
public void onFacebookError(FacebookError e)
{
}
public void onError(DialogError e)
{
}
public void onComplete(Bundle values)
{
SessionStore.save(mFb, getApplicationContext());
}
public void onCancel()
{
// TODO Auto-generated method stub
}
});
and this is Facebook Connector Class....
public class FacebookConnector
{
private Facebook facebook = null;
private Context context;
private String[] permissions;
private Handler mHandler;
private Activity activity;
//private SessionListener mSessionListener = new SessionListener();;
public FacebookConnector(String appId,Activity activity,Context context,String[] permissions)
{
this.facebook = new Facebook(appId);
SessionStore.restore(facebook, context);
this.context=context;
this.permissions=permissions;
this.mHandler = new Handler();
this.activity=activity;
}
public void login()
{
if (!facebook.isSessionValid())
{
facebook.authorize(this.activity, this.permissions,new LoginDialogListener());
}
}
/*public void logout()
{
SessionEvents.onLogoutBegin();
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
asyncRunner.logout(this.context, new LogoutRequestListener());
}*/
public void postMessageOnWall(String msg)
{
if (facebook.isSessionValid())
{
Bundle parameters = new Bundle();
parameters.putString("message", msg);
try
{
//JSONObject response=Util.parseJson(facebook.request("me/feed", parameters,"POST"));
String response = facebook.request("me/feed", parameters,"POST");
System.out.println(response);
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
}
}
private final class LoginDialogListener implements DialogListener
{
public void onComplete(Bundle values)
{
SessionEvents.onLoginSuccess();
}
public void onFacebookError(FacebookError error)
{
SessionEvents.onLoginError(error.getMessage());
}
public void onError(DialogError error)
{
SessionEvents.onLoginError(error.getMessage());
}
public void onCancel()
{
SessionEvents.onLoginError("Action Canceled");
}
}
private class SessionListener implements AuthListener, LogoutListener
{
public void onAuthSucceed()
{
SessionStore.save(facebook, context);
}
public void onAuthFail(String error) {
}
public void onLogoutBegin() {
}
public void onLogoutFinish() {
SessionStore.clear(context);
}
}
public Facebook getFacebook()
{
return this.facebook;
}
}

Facebook Integration with Android App Error

I am trying to integrate Facebook ** into my android app. I want to implement **like and share ** facility on **facebook. But i am getting error.
please help me to solve this.
Also I want to know the process of creating New App on Facebook.
I am using code from GitHub. My code is -
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("333778590046892");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void save(String token){
Toast.makeText(this, "token"+token, Toast.LENGTH_LONG).show();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
}
I am getting the following error while running the above code -
Dialog Errorcom.facebook.android.DialogError: The connection to the server was unsuccessful.
try this Single Sign On (SSO) Using Android Native Client For Facebook .
paste this
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
instead of this
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}

Issue with Android Facebook SDK

in my activity I use the following code to get Facebook authorization. facebook.authorize is called but it does not enter in one of the listener event (onComplete, onFacebookError...). Anybody could help?
Facebook facebook = new Facebook(FACEBOOK_APPID);
facebook.authorize(ItemDetail.this, new String[] {"publish_stream"},
new DialogListener() {
#Override
public void onComplete(Bundle values) {
Log.i("ItemDetail", "Facebook onComplete");
}
#Override
public void onFacebookError(FacebookError error) {
Log.i("ItemDetail", "Facebook onFacebookError");
}
#Override
public void onError(DialogError e) {
Log.i("ItemDetail", "Facebook onError");
}
#Override
public void onCancel() {
Log.i("ItemDetail", "Facebook onCancel");
}
}
);
You need to put this in your class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
}

Categories

Resources