开发者

Facebook C# SDK and Access Token

开发者 https://www.devze.com 2023-02-14 15:16 出处:网络
I\'d like to be able to authenticate myself (my profile, not just my app) on my own web application using the Faceboo开发者_JS百科k C# SDK. Using the Graph API, I can get an access token, but that tok

I'd like to be able to authenticate myself (my profile, not just my app) on my own web application using the Faceboo开发者_JS百科k 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


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.


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.


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 !!

0

精彩评论

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

关注公众号