简体   繁体   English

Android Facebook ..如何获得AccessToken

[英]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. 嗨,我想添加Facebook连接到我的Android应用程序,我确实设法发布在我的墙上的应用程序,但当我下载Android Facebook应用程序并登录它但现在我不能发布在我的墙上。 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 { 公共类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();
    }

} } }}

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. 您还必须首次登录Facebook才能使用您的应用并授予该应用的权限。

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 为此,请查看Facebook_Android_SDK中的示例应用程序LoginButton.java init()方法

Note: make a boolean flag stored in your sharedPreferences to indicate if this is the first time or not, 注意:在sharedPreferences中存储一个布尔标志,以指示这是否是第一次,
so the login dialog doesn't pop up every time you use the application. 因此,每次使用该应用程序时都不会弹出登录对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM