Can't figure out how to get access token using facebook c# sdk

不想你离开。 提交于 2019-12-14 04:21:01

问题


I'm currently writing a Facebook app that runs in a page tab. I'm writing it as a simple Web Forms app in C#, using the latest version of the C# SDK pulled from NuGet. The main page tab works fine. I get all the info I need from FacebookWebContext.Current.SignedRequest, and I'm fine there. I'm trying to write a page now that the page admin would use to set up the app, so this is the page that would go under the "Edit URL" in the app setup.

All I really want to do is get the currently signed-on user's ID, and determine if he's an admin for the page in question.

I'm doing this:

var client = new FacebookClient();
dynamic me = client.Get("me");

in Page_Load, and getting the following exception:

(OAuthException) An active access token must be used to query information about the current user.

I've tried a bunch of stuff to get the access token, but I don't seem to know what I'm doing wrong. Is there a straightforward way of doing this?

Update: I figured out at one point that I had a reference to the old JS SDK on my master page, and that was causing a problem. (See here). Removing that got me to the point where I was usually able to see whether or not the user was actually logged in to Facebook.


回答1:


Try this:

var fbContext = FacebookWebContext.Current;
if (fbContext.IsAuthenticated())
{
    var client = new FacebookClient(fbContext.AccessToken);
    dynamic me = client.Get("me");
}



回答2:


As far as I understand, being logged-in in Facebook does not mean FacebookWebcontext.Current.isAuthenticated() == true. What Facebook wants to make sure is that the user has authorized the app. So what you should do is to forward the page to facebook authorization dialogue page (https://www.facebook.com/dialog/oauth?) with necessary permissions. Facebook will determine whether the app has been authorized by the user and redirect to the redirect_uri with a newer access_token issued.



来源:https://stackoverflow.com/questions/6221004/cant-figure-out-how-to-get-access-token-using-facebook-c-sharp-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!