2011-05-06 77 views
7

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

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

CODE:

公共类FacebookLogin延伸活动{

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(); 
    } 

}}

令牌,我得到的是空。

回答

2

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

您还必须首次登录Facebook才能使用您的应用并授予应用权限。

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

然后你应该存储你的会话,当再次使用应用程序时,你不会得到这个错误。

要做到这一点,请从Facebook_Android_SDK

注意查看示例应用程序LoginButton.java init()方法:使存储在您sharedPreferences一个布尔标志,表示如果这是第一次还是不行,
所以每次使用应用程序时都不会弹出登录对话框。

+0

谢谢,但我不明白在哪里即时获取accessToken ..如果我做facebook.authorize(这,新字符串[] {“publish_stream”},.. 我得到一个身份证在回报,但它不是一个令牌,我试过了你怎么获得sessionId或TokenId ...谢谢 – user606669 2011-05-10 10:56:45

+0

问题通过在facebook.authorize的onComplete函数中调用getaccesstoken解决,并将其存储在首选项中,以便每次检查首选项并从那里加载访问令牌 – user606669 2011-05-10 13:40:23

相关问题