Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook .. how to get AccessToken

Hi I am trying to add facebook connect to my Android app , I did managed to post on my wall with app but when I downloaded android facebook app and logged in on it but now I cant post on my wall. I am getting this error:

{"error":{"type":"OAuthException","message":"An active access token 
must be used to query information about the current user."}}

CODE:

public class FacebookLogin extends Activity {

private static final String APP_API_ID = "080808998-myappId";
Facebook facebook = new Facebook(APP_API_ID);
private AsyncFacebookRunner mAsyncRunner;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    

    mAsyncRunner = new AsyncFacebookRunner(facebook);

    SessionStore.restore(facebook, this);
    SessionEvents.addAuthListener(new SampleAuthListener());
    Log.i("Error", facebook.getAccessToken()+"tokenId");
    facebook.dialog(FacebookLogin.this, "feed", new SampleDialogListener());

}

public void postOnWall(String msg) {
    try {
           Bundle bundle = new Bundle();
           bundle.putString("message", msg);
           bundle.putString("from", "fromMe");
           bundle.putString("link","facebook.com");
           bundle.putString("name","name of the link facebook");
           bundle.putString("description","some description here");

           //bundle.putString(Facebook.TOKEN, accessToken);

           bundle.putString("picture", "http://url to image");
           String response = facebook.request("me/feed",bundle,"POST");

           Log.d("Error", "got response: " + response);
           if (response == null || response.equals("") || 
                   response.equals("false")) {
              Log.v("Error", "Blank response");
           }
    } catch(Exception e) {
        e.printStackTrace();
    }

} }

THe token I am getting is null.

like image 992
user606669 Avatar asked May 06 '11 14:05

user606669


1 Answers

You didn't check if the session is valid or not.
If not, then you have to authorize.

You also have to sign in to Facebook the first time to use your app and give the permissions to the app.

Then you will get the access token for the first time.

Then you should store your session and when using the app again you will not get this error.

To do so, please review the Example application LoginButton.java init() method from Facebook_Android_SDK

Note: make a boolean flag stored in your sharedPreferences to indicate if this is the first time or not,
so the login dialog doesn't pop up every time you use the application.

like image 57
Amal Avatar answered Sep 21 '22 13:09

Amal