Android Facebook ..如何获得AccessToken

use*_*669 7 android facebook

嗨,我想添加Facebook连接到我的Android应用程序,我确实设法发布在我的墙上的应用程序,但当我下载Android Facebook应用程序并登录它但现在我不能发布在我的墙上.我收到此错误:

{"error":{"type":"OAuthException","message":"An active access token 
must be used to query information about the current user."}}
Run Code Online (Sandbox Code Playgroud)

码:

公共类FacebookLogin扩展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();
    }
Run Code Online (Sandbox Code Playgroud)

}}

我获得的令牌是空的.

Ama*_*mal 2

您没有检查会话是否有效。
如果没有,那么您必须授权。

您还必须在首次使用您的应用程序时登录 Facebook 并授予该应用程序权限。

然后您将第一次获得访问令牌。

然后您应该存储您的会话,再次使用该应用程序时您将不会收到此错误。

为此,请查看Facebook_Android_SDK中的示例应用程序 LoginButton.java init() 方法

注意:在您的sharedPreferences中存储一个布尔标志来指示这是否是第一次,
这样您每次使用该应用程序时就不会弹出登录对话框。