如何在C#中获取Facebook Graph API的访问令牌?

时间:2012-12-13 09:08:04

标签: facebook c#-4.0 facebook-graph-api

我正在尝试用C#开发一个演示Facebook App。但我无法获得访问令牌(适用于应用程序和用户)。

请帮助我,我怎么能同时获得这个应用程序&用户(欢迎使用Diff示例)。

我的代码是:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        CheckIfFacebookAppIsSetupCorrectly();

        var fbWebContext = FacebookWebContext.Current;
        if (fbWebContext.IsAuthorized())
        {
            var fb = new FacebookWebClient(fbWebContext);
            dynamic result = fb.Get("/me");

            lblName.Text = "Hi " + result.name;
        }
    }
    private void CheckIfFacebookAppIsSetupCorrectly()
    {
        bool isSetup = false;
        var settings = ConfigurationManager.GetSection("facebookSettings");
        if (settings != null)
        {
            var current = settings as IFacebookApplication;
            if (current.AppId != "{app id}" &&
                current.AppSecret != "{app secret}")
            {
                isSetup = true;
            }
        }

        if (!isSetup)
        {
            System.Web.HttpContext.Current.Response.Redirect("~/GettingStarted.aspx");
        }
    }
}

当我通过输入断点来检查代码时,我发现 if(fbWebContext.IsAuthorized())总是返回false,如果我尝试对Authorization进行注释,我会得到以下异常:

(OAuthException)必须使用活动访问令牌来查询有关当前用户的信息。

我搜索了这个,我得到了以下链接:Another Question Link

但是当我获得应用程序的访问令牌时,我无法分配给该对象,因为它是只读的。

在这种情况下我该怎么做,另外我如何获得用户的访问令牌?

由于

1 个答案:

答案 0 :(得分:0)

您需要在facebookDeveloperSite

创建新的应用和帐户

您将从那里获得访问令牌

此致