简体   繁体   English

Facebook C#SDK获得用户accessToken

[英]Facebook C# SDK get user accessToken

I having problems with getting the accessToken from the results of my login to facebook. 我从登录到Facebook的结果获取accessToken时遇到问题。 In the Page_load, my url doesn't get parsed correctly. 在Page_load中,我的网址无法正确解析。

GenerateLoginUrl method is working without errors and i can see the accessToken in the QueryString. GenerateLoginUrl方法正常工作,没有错误,我可以在QueryString中看到accessToken。 I have tried get it from the but that it's not working either. 我尝试从中获取它,但是它也不起作用。

    protected void Page_Load(object sender, EventArgs e)
{
    var fb = new FacebookClient();

    FacebookOAuthResult result;
    if (fb.TryParseOAuthCallbackUrl(Request.Url, out result))
    {
        if (result.IsSuccess)
        {
            // result clicked authorized
            var accessToken = result.AccessToken;
            Label1.Text = accessToken.ToString();
        }
        else
        {
            // user clicked cancelled and didn't authorize the app
            var error = result.ErrorDescription;
            Label1.Text = error;
        }
    }
    else
    {
        //This is were it always goes
        Label1.Text = "not valid facebook url";
    }


}


private Uri GenerateLoginUrl(string appId, string extendedPermissions)
{

    try
    {
        dynamic parameters = new ExpandoObject();
        parameters.client_id = appId;
        parameters.redirect_uri = "local iis ip";

        // The requested response: an access token (token), an authorization code (code), or both (code token).
        parameters.response_type = "token";

        // list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
        parameters.display = "popup";

        // add the 'scope' parameter only if we have extendedPermissions.
        if (!string.IsNullOrWhiteSpace(extendedPermissions))
            parameters.scope = extendedPermissions;

        // generate the login url
        var fb = new FacebookClient();

       var url = fb.GetLoginUrl(parameters);

       return url;
    }

    catch (FacebookOAuthException fbex)
    {
        Label1.Text = fbex.Message;
        return null;

    }

    catch (Exception ex)
    {
        Label1.Text = ex.Message;
        return null;
    }


}


protected void Button1_Click(object sender, EventArgs e)
{
    string AppId = ConfigurationManager.AppSettings["apiKey"];
    string ExtendedPermissions = ConfigurationManager.AppSettings["ExtendedPermissions"];

    var url = GenerateLoginUrl(AppId, ExtendedPermissions);

    Response.Redirect(url.AbsoluteUri, false);
}

Thanks for your help. 谢谢你的帮助。

Update: 更新:

In the Request.Url the url always is: 在Request.Url中,URL始终为:

http://localhost:23560/Appname/default.aspx

but i can se the real url in the browser and it is: 但是我可以在浏览器中找到真正的网址,它是:

http://localhost:23560/Appname/#access_token=0000000000&expires_in=3821

So way can't asp.net read the url correctly? 因此,asp.net无法正确读取网址吗?

I found the answare and it is: 我找到了answare,它是:

You have to get the code not token, the code is gonna show in querystring accesstoken gets stripped. 您必须获取代码而不是令牌,该代码将显示在querystring accesstoken中。

 parameters.response_type = "token";

exchange 交换

 parameters.response_type = "code";

And then exchange to code to token 然后交换代码到令牌

 dynamic token = fb.Get("oauth/access_token", new
                {
                    client_id = "appid",
                    client_secret = "appsecret",
                    redirect_uri = url,
                    code = Request.QueryString["code"]
                });

var accessToken = token.access_token; var accessToken = token.access_token;

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

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