(Facebook C# SDK) Problem getting an access token

五迷三道 提交于 2019-12-01 22:55:25
jimmystormig

I think the problem is that you never get a User Access Token you only get a App Access Token. In a standalone app you need to use the oAuth Dialog. The simplest way to do this is using the Javascript SDK.

Facebook C# SDK has a sample that shows you how to do this. You could download the whole sample app (CSASPNETWebsite) as a startingpoint.

In both the code request and access token request the redirect_uri must be the same, this will fix the OAuthException in the end. My code is now something like:

...    
if (result.IsSuccess)
{
    var tokenresult = cl.ExchangeCodeForAccessToken(result.Code, new Dictionary<string, object> { { "redirect_uri", "<your_redirect_uri>" } });
}
...

You code is correct for the most part, the only piece you were missing was the "redirect_uri" parameter when calling the "ExchangeCodeForAccessToken" method. This has to match the App URL you set up when you registered your Facebook App. Otherwise, you will get the error you were receiving. You don't need the "permissions" parameter, you have already received that during the prior authorization.

 Dictionary<string, object> parameters = new Dictionary<string, object>();
 parameters.Add("redirect_uri", "http://www.yourcallbackurl.com/");
 var result = cl.ExchangeCodeForAccessToken(result.code,parameters);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!