Facebook Session.openActiveSessionWithAccessToken即使在Android中使用有效令牌也无效

时间:2014-03-07 18:41:14

标签: java android facebook facebook-graph-api

我正在使用Android和Facebook API,基本上,我想要的是创建一个警报,每隔一定时间做一些Facebook的东西,即使用户已经启动他的设备并且应用程序没有在系统中运行

我的FacebookLoginFragment正在以这种方式完成登录Facebook登录按钮:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    loginDone = false;

    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);

    Session.setActiveSession(new Session.Builder(getActivity().getBaseContext()).setApplicationId(getString(R.string.app_id)).build());

    return inflater.inflate(R.layout.fragment_facebook_login, container, false);
}

这是有效的,这是处理程序:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (session.isOpened()) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        if (session.getAccessToken() != null)
            editor.putString("com.facebook.sdk.AccessToken", session.getAccessToken());

        if (session.getExpirationDate()!= null)
            editor.putLong("com.facebook.sdk.AccessTokenExpires", session.getExpirationDate().getTime());

        editor.commit();

        if (!loginDone) {
            loginDone = true;
            startActivity(new Intent(getActivity(), UpdateSnitchListActivity.class));
        }
    }
}

用户授权应用程序后我正在做的是保存身份验证令牌。然后,在我的BroadcastReceiver中,我想访问Facebook的东西,但它失败了:

public class MyBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.i("ALARM", "MyBootReceiver is executing when device boots");
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

            if (sharedPreferences.getBoolean(context.getString(R.string.preference_use_service), true)) {
                if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) {
                    String accessTokenString = sharedPreferences.getString("com.facebook.sdk.AccessToken", null);
                    Long accessTokenExpires = sharedPreferences.getLong("com.facebook.sdk.AccessTokenExpires", 0);

                    Session.setActiveSession(new Session.Builder(context).setApplicationId(context.getString(R.string.app_id)).build());
                    AccessToken accessToken = AccessToken.createFromExistingAccessToken(accessTokenString, new Date(accessTokenExpires), null, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, null);
                    Session.openActiveSessionWithAccessToken(context, accessToken, null);
                    // And then here I get the user's friends list and fail
                }
            }
        }
    }
}

这是我每次尝试时都会遇到的错误:

Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 104, errorType: OAuthException, errorMessage: An access token is required to request this resource.}, isFromCache:false

我不知道是什么问题,因为我提供了访问令牌,访问令牌到了我从用户登录中获得的,当用户正常登录时,它可以正常工作。

提前致谢!

1 个答案:

答案 0 :(得分:1)

尝试使用String来保存accessTokenExpires然后以这种方式获取:

SimpleDateFormat format = new SimpleDateFormat( "ccc MMM dd HH:mm:ss ZZZZ yyyy", Locale.ENGLISH );
try {
    Date expireDate = format.parse( accessTokenExpires );
} catch ( java.text.ParseException e ) {
    e.printStackTrace();
}

我的工作代码:

SimpleDateFormat format = new SimpleDateFormat( "ccc MMM dd HH:mm:ss ZZZZ yyyy", Locale.ENGLISH );

try {
    Date expireDate = format.parse( accessExpireStr );
    AccessToken accessToken = AccessToken.createFromExistingAccessToken( accessTokenStr, expireDate, null, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, mPermissions );
    Session.openActiveSessionWithAccessToken( mActivity, accessToken, mFacebookCallback );
} catch ( java.text.ParseException e ) {
    e.printStackTrace();
}

其中

List<String> mPermissions = new ArrayList<String>();
mPermissions.add( "public_profile" );
mPermissions.add( "email" );