0

我正在尝试检索我的 Facebook 页面信息。但是当我第一次安装并运行该应用程序时,它给了我。

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

下次我运行应用程序时,它会返回正确的结果。为什么会这样。我希望它在安装后第一次运行应用程序时返回数据。

这是我的 Facebook sdk 代码:

private String access_Token="";
private final String APP_ID="MY_APP_ID";
private final String[] PERMS = new String[] { "publish_stream","manage_pages" };
private Bundle params=new Bundle();
private SharedPreferences sharedPrefs;
private AsyncFacebookRunner mAsyncRunner;
private Facebook mfacebook;
private TextView view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view=(TextView)findViewById(R.id.Mozi);

    this.SetConnection();                               //Initialize Fb
    this.getAccessToken();                              //GetAccessToken
    this.CheckSessionExpiry();                          //Create Session with permissions if expired
    this.RetrieveUserPages();
//  this.EnableFBLogout();
}

public void onResume() {
    super.onResume();
    mfacebook.extendAccessTokenIfNeeded(this, null);
}

private void EnableFBLogout()
{
    mAsyncRunner.logout(getApplicationContext(), new RequestListener() {
          @Override
          public void onComplete(String response, Object state) {

                 String method = "DELETE";
                 Bundle params = new Bundle();
                 /*
                  * this will revoke 'publish_stream' permission
                  * Note: If you don't specify a permission then this will de-authorize the application completely.
                  */
                 params.putString("permission", "publish_stream");
                 mAsyncRunner.request("/me/permissions", params, method,new RequestListener() {

                    @Override
                    public void onMalformedURLException(MalformedURLException e, Object state) {
                        // TODO Auto-generated method stub
                        Log.e("PerMalform",e.getMessage());
                    }

                    @Override
                    public void onIOException(IOException e, Object state) {
                        // TODO Auto-generated method stub
                        Log.e("PerMalform",e.getMessage());

                    }

                    @Override
                    public void onFileNotFoundException(FileNotFoundException e, Object state) {
                        // TODO Auto-generated method stub
                        Log.e("PerMalform",e.getMessage());

                    }

                    @Override
                    public void onFacebookError(FacebookError e, Object state) {
                        // TODO Auto-generated method stub
                        Log.e("PerMalform",e.getMessage());

                    }

                    @Override
                    public void onComplete(String response, Object state) {
                        // TODO Auto-generated method stub
                        Log.e("PerMalform",response);

                    }
                }, null);

          }

          @Override
          public void onIOException(IOException e, Object state) {}

          @Override
          public void onFileNotFoundException(FileNotFoundException e,
                Object state) {}

          @Override
          public void onMalformedURLException(MalformedURLException e,
                Object state) {}

          @Override
          public void onFacebookError(FacebookError e, Object state) {}
        });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    this.mfacebook.authorizeCallback(requestCode, resultCode, data);
}

private void getAccessToken()
{
    sharedPrefs= getPreferences(MODE_PRIVATE);
    String access_token = sharedPrefs.getString("access_token", null);
    long expires = sharedPrefs.getLong("access_expires", 0);
    if(access_token != null) {
        mfacebook.setAccessToken(access_token);
    }
    if(expires != 0) {
        mfacebook.setAccessExpires(expires);
    }
}

private void CheckSessionExpiry()
{
      if(!mfacebook.isSessionValid()) {

          mfacebook.authorize(this, this.PERMS , new DialogListener() {
              @Override
              public void onComplete(Bundle values) {
                  SharedPreferences.Editor editor = sharedPrefs.edit();
                  editor.putString("access_token", mfacebook.getAccessToken());
                  editor.putLong("access_expires", mfacebook.getAccessExpires());
                  editor.commit();
              }

              @Override
              public void onFacebookError(FacebookError error) {
                  Log.e("mozi1",error.toString());
              }

              @Override
              public void onError(DialogError e) {
                  Log.e("mozi2",e.toString());

              }

              @Override
              public void onCancel() { 

                  Log.e("sad","ww");
              }
          });
      }
  }

private void SetConnection()

{
    this.mfacebook=new Facebook(this.APP_ID);
    this.mAsyncRunner=new AsyncFacebookRunner(mfacebook);
}

private void  RetrieveUserPages()
{
    this.params.putString(Facebook.TOKEN, mfacebook.getAccessToken());
    this.mAsyncRunner.request("me/accounts", this.params, "GET", new RequestListener() {

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) {
            // TODO Auto-generated method stub
            Log.e("Malformed",e.getMessage());

        }

        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub
            Log.e("IO",e.getMessage());

        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) {
            // TODO Auto-generated method stub
            Log.e("FNF",e.getMessage());

        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
            Log.e("FBERR",e.getMessage());

        }

        @Override
        public void onComplete(String response, Object state) {
            // TODO Auto-generated method stub
            Log.i("responsefromFB",response);       //here the response is an error for the first time and data the second time.
        //  view.setText(response);                             
        }
    }, null);
}
4

1 回答 1

0

mfacebook.authorize()是异步的,这意味着authorize()方法运​​行之后的代码即使authorize()没有完成。因为你this.RetrieveUserPages()马上打电话mfacebook.authorize(),你在打电话

    this.params.putString(Facebook.TOKEN, mfacebook.getAccessToken());

before mfacebook.authorize()已经完成,所以mfacebook.getAcessToken()返回null。

您的问题的答案是调用方法以确保在尝试检索之前设置您的访问令牌this.RetrieveUserPages()onCompletemfacebook.authorize()

让我知道这是否有帮助!

于 2012-08-21T18:47:30.233 回答