开发者

Android Facebook .. how to get AccessToken

开发者 https://www.devze.com 2023-03-03 23:32 出处:网络
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 androi开发者_运维百科d facebook app and logged in on it but now I cant post o

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 androi开发者_运维百科d 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消