Facebook C# SDK and Access Token

99封情书 提交于 2019-11-27 23:27:36

问题


I'd like to be able to authenticate myself (my profile, not just my app) on my own web application using the Facebook C# SDK. Using the Graph API, I can get an access token, but that token does not seem to work properly with the Facebook C# as it seems to be stateless.

The error that is thrown is:

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

I've poked around the Facebook C# SDK and documentation and most of the info I'm seeing is to redirect users to a login page which is not what I'm looking for.

Does anyone have a good sample of auto-logging in myself so I can pull up my own information?

TIA


回答1:


When you say "yourself" do you mean the app or your actual facebook user?

If it's just your app, you can get an access token by POSTing to this URL:

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID_HERE&client_secret=APP_SECRET_HERE

You can use this access token to perform actions on behalf of your users if they have authorized your app to do so.




回答2:


I had the same problem where the access token didn't include the session part. Please check out my answer to this similar question - exchange code for token facebook-c#-sdk.

Here's a sample from my Home controller

[CanvasAuthorize]
public ActionResult Index()
{
    var app = new FacebookClient(new Authorizer().Session.AccessToken);

    dynamic me = app.Get("me");
    ViewBag.Firstname = me.first_name;
    ViewBag.Lastname = me.last_name;

    return View();
}

Edit

Now I understand the question better. What about this?

var auth = new Authorizer(FacebookContext.Current.AppId, FacebookContext.Current.AppSecret, HttpContext);

auth.Authorize();

From the documentation:

public bool Authorize() Member of Facebook.Web.Authorizer

Summary: Authorizes the user if the user is not logged in or the application does not have all the sepcified permissions.

Returns: Return true if the user is authenticated and the application has all the specified permissions.




回答3:


I dont know if this will work for you:

using Facebook.Web;
using Facebook;
using System.Dynamic;

var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me"} };

if (auth.Authorize())
{
}

and include this in the aspx:

<input type="hidden" name="signed_request" value="<%: Request.Params["signed_request"]%>"/>

good luck !!



来源:https://stackoverflow.com/questions/5171309/facebook-c-sharp-sdk-and-access-token

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