简体   繁体   English

Facebook C#SDK和访问令牌

[英]Facebook C# SDK and Access Token

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. 我希望能够使用Facebook C#SDK在我自己的Web应用程序上验证自己(我的个人资料,而不仅仅是我的应用程序) 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. 使用Graph API,我可以获得访问令牌,但该令牌似乎与Facebook C#无法正常工作,因为它似乎是无状态的。

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. 我浏览了Facebook C#SDK和文档,我看到的大部分信息都是将用户重定向到登录页面,这不是我想要的。

Does anyone have a good sample of auto-logging in myself so I can pull up my own information? 有没有人有自己的自动记录样本,所以我可以提取自己的信息?

TIA TIA

When you say "yourself" do you mean the app or your actual facebook user? 当你说“你自己”时,你的意思是应用程序或你的实际Facebook用户?

If it's just your app, you can get an access token by POSTing to this URL: 如果它只是您的应用程序,您可以通过POST到此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 . 请查看我对这个类似问题的回答 - 令牌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 public bool Authorize()Facebook.Web.Authorizer的成员

Summary: Authorizes the user if the user is not logged in or the application does not have all the sepcified permissions. 摘要:如果用户未登录或应用程序未具有所有sepcified权限,则授权用户。

Returns: Return true if the user is authenticated and the application has all the specified permissions. 返回:如果用户经过身份验证且应用程序具有所有指定的权限,则返回true。

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: 并将其包含在aspx中:

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

good luck !! 祝好运 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM