得到使用Facebook的C#SDK访问令牌 [英] get an access token using Facebook C# SDK

查看:554
本文介绍了得到使用Facebook的C#SDK访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码使用C#SDK

Hi i am using the following code to get access token from facebook using C# SDK

var fb = new FacebookClient();
    dynamic result = fb.Get("oauth/access_token", new
    {
        client_id = "clientId",
        client_secret = "clientSecret",
        redirect_uri = "redirectUri",
        code = "code"
    });

    return result.access_token;



上面的代码可以完美运行的大部分时间,但有些时候,我得到这个错误

the above code works perfect most of the time but some times i gets this error

(OAuthException - #100) Invalid verification code format.



如何解决这个问题?

how to fix this problem??

推荐答案

什么是您的项目类型:?的WinForms WPF ASP.NET

What is your project type : WinForms , WPF , ASP.NET ?

如果您是的WinForms 或工作的 WPF ,你必须让的access_token 通过请求的OAuth登录对话框和 return_type =标记,然后提取有效形成浏览器控件网址的access_token 从URL。

if you are working with WinForms or WPF , you have to get the access_token form the Browser Control URL by requesting the OAuth Login Dialog and the return_type=token , then extract the valid access_token from the URL.

另外,如果你的Web应用程序中使用工作的 ASP.NET ,你将不得不将用户重定向到的OAuth对话框登录页面那么Facebook将您重定向回来的URL代码,您从查询字符串并作出的HTTPRequest 来Facebook的得到有效的的access_token

Otherwise , if you are working on Web Application using ASP.NET , you will have to redirect the user to the OAuth Dialog Login Page then the facebook will redirect you back with a code on the URL , you get this code from the QueryString and make an HTTPRequest to the Facebook to get the valid access_token .

您可以用我的方法,这样做的:

you can use my method for doing that :

 public string GetAccessTokenFromCode(string AppID, string AppSecret, string RedirectURL, string Code)
{
WebClient wc = new WebClient();
string u2 = "https://graph.facebook.com/oauth/access_token?client_id=" + AppID + "&redirect_uri=" + RedirectURL + "&client_secret=" + AppSecret + "&code=" + Code + "&state=anytexthere";
string access = wc.DownloadString(u2);
access = access.Substring(access.IndexOf("access_token") + 13);
if (access.Contains("&"))
{
string accesstoken = access.Substring(0, access.IndexOf("&"));
return accesstoken;
}

return access;

}

和您可以从的Page_Load

if (Request.QueryString["code"] != null)
{
code = Request.QueryString["code"].ToString();
string AT = GetAccessTokenFromCode(AppID, AppSecret, RedirectUrl, Code);
}

这篇关于得到使用Facebook的C#SDK访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆