开发者

Using the Facebook C# API - validating that a stored access token is valid

I have an access token that I stored - I want to use it to post to facebook on behalf of the user. Is there any way to开发者_开发技巧 validate that the token is still valid before letting the user post so that I can pop the challenge UI again?


Are you requesting offline_access? If not, the token will expire, but querying for the token should return the expiration date. You can check it on your side before making the call.

Validating the user has not removed permissions or validating offline_access I believe would require hitting the Facebook servers. I would just try the post and handle the error if it fails. It's probably more likely than not that the token is valid.

By checking before every call, you are incurring a significant penalty (double the work) to handle the infrequent case. You'd still have to prompt them anyway if the access token is invalid, so I'd just handle the error and prompt after.

Besides, you still have to handle the error anyway, since there is a race condition. You can check the access_token, but in the time between checking and posting, the token could become invalid.


From my experience (using 4.2), you will need to wrap your request in a try.. catch (an OAuthException might fire). And I am talking about the scenario where you have offline access. (If you have offline access and the user removes the app within Facebook then your offline token will expire -- even if the user re-enables the app through some other mechanism).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜