0

我能够使用我在 android 上的应用程序连接到 Facebook,并查看我的姓名、姓氏等字段。然后我去Facebook.com我的电脑上,从允许的应用程序中删除我的应用程序,然后重新启用(用于测试)。

然后,当我运行我的应用程序时,我在 Logcat 中收到以下错误:

FacebookError: Error validating access token: Session does not match current stored session.

这可能是因为用户在创建会话后更改了密码,或者 Facebook 出于安全原因更改了会话。

我尝试了一切,搜索了论坛 - 但没有运气。

我的代码:

public static final String TAG = "FACEBOOK";
private Facebook mFacebook;
public static final String APP_ID = "00112233211545";// I changed this when i publish to Stackoverflow
private AsyncFacebookRunner mAsyncRunner;
private static final String[] PERMS = new String[] { "read_stream" };
private SharedPreferences sharedPrefs;
private Context mContext;

private TextView username;
private ProgressBar pb;

public void setConnection() {
    mContext = this;
    mFacebook = new Facebook(APP_ID);

    mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    mFacebook.extendAccessTokenIfNeeded(this, null);
}

public void getID(TextView txtUserName, ProgressBar progbar) {
    username = txtUserName;
    pb = progbar;
    if (isSession()) {
        Log.d(TAG, "sessionValid");
        mAsyncRunner.request("me", new IDRequestListener());
    } else {
        // no logged in, so relogin
        Log.d(TAG, "sessionNOTValid, relogin");
        mFacebook.authorize(this, PERMS, new LoginDialogListener());
    }
}

public boolean isSession() {
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    String access_token = sharedPrefs.getString("access_token", "x");
    Long expires = sharedPrefs.getLong("access_expires", -1);
    Log.d(TAG, access_token);

    if (access_token != null && expires != -1) {
        mFacebook.setAccessToken(access_token);
        mFacebook.setAccessExpires(expires);
    }
    return mFacebook.isSessionValid();
}

private class LoginDialogListener implements DialogListener {

    public void onComplete(Bundle values) {
        Log.d(TAG, "LoginONComplete");
        String token = mFacebook.getAccessToken();
        long token_expires = mFacebook.getAccessExpires();
        Log.d(TAG, "AccessToken: " + token);
        Log.d(TAG, "AccessExpires: " + token_expires);
        sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        sharedPrefs.edit().putLong("access_expires", token_expires)
        .commit();
        sharedPrefs.edit().putString("access_token", token).commit();
        mAsyncRunner.request("me", new IDRequestListener());
    }

    public void onFacebookError(FacebookError e) {
        Log.d(TAG, "FacebookError: " + e.getMessage());
    }

    public void onError(DialogError e) {
        Log.d(TAG, "Error: " + e.getMessage());
    }

    public void onCancel() {
        Log.d(TAG, "OnCancel");
    }
}

private class IDRequestListener implements RequestListener {

    public void onComplete(String response, Object state) {
        try {
            Log.d(TAG, "IDRequestONComplete");
            Log.d(TAG, "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);

            final String id = json.getString("id");
            final String name = json.getString("name");
            osnovni_meni.this.runOnUiThread(new Runnable() {
                public void run() {
                    username.setText("Welcome: " + name+"\n ID: "+id);
                    pb.setVisibility(ProgressBar.GONE);

                }
            });
        } catch (JSONException e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        } catch (FacebookError e) {
            Log.d(TAG, "FacebookError: " + e.getMessage());
        }
    }

    public void onIOException(IOException e, Object state) {
        Log.d(TAG, "IOException: " + e.getMessage());
    }

    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        Log.d(TAG, "FileNotFoundException: " + e.getMessage());
    }

    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        Log.d(TAG, "MalformedURLException: " + e.getMessage());
    }

    public void onFacebookError(FacebookError e, Object state) {
        Log.d(TAG, "FacebookError: " + e.getMessage());
    }

}

protected void onActivityResult1(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}

public void facebook_login(View view) throws IOException, JSONException
{
    TextView txtUserName = (TextView) findViewById(R.id.txtUserName);
    ProgressBar pbLogin = (ProgressBar) findViewById(R.id.pbLogin);
    pbLogin.setVisibility(ProgressBar.VISIBLE);
    setConnection();

    getID(txtUserName, pbLogin);


}
4

1 回答 1

0

你最有可能得到isSessionValid = true- 你需要忽略并运行它mFacebook.authorize

于 2012-10-04T20:34:39.863 回答