2013-02-20 49 views
2

嗨,我使用下面的代码使用C#SDK摆脱Facebook的访问令牌的访问令牌得到使用Facebook的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; 

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

(OAuthException - #100) Invalid verification code format. 

如何解决这个问题?

回答

0

您应该拥有与询问code时一样的redirect_uri
此外,在Facebook上的“在Facebook上登录网站”部分配置的网站网址结尾处必须有一个尾部的斜杠'/'。
这里有一个完整的教程:Working with C# SDK

+0

请详细说明何时发生此异常。尝试使用“发布”请求而不是“获取”。 – ThePCWizard 2013-02-20 12:43:28

+0

以及我不知道我在我的网站上有它,并得到techmails lods为这个错误。当我测试它或要求我的朋友测试链接出来的每件事情都适用于我,但科技邮件不断来自一些访客 – 2013-02-20 12:51:50

+0

您可以发布您通过哪种方法获得代码值 – ThePCWizard 2013-02-20 13:10:49

4

什么是您的项目类型:的WinFormsWPFASP.NET

,如果你是的WinForms工作WPF,你必须通过请求的OAuth登录对话框和return_type=token得到access_token形式Browser Control URL,然后从该网址提取有效access_token。否则,如果您正在使用ASP.NET正在使用Web应用程序,则必须将用户重定向到OAuth对话框登录页面,然后Facebook会使用URL上的代码重定向您,您会得到代码从QueryString并作出HTTPRequest到Facebook以获得有效的access_token

你可以使用我的方法做这件事:

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); 
} 
+0

如何获取AppId, AppSecret和Code值? – Kiquenet 2014-11-13 21:19:07

-1

http://www.nuget.org/packages/Facebook.CSharp.SDK/下载SDK

var config = new Dictionary<string, object>(); 
//your application id and secret from https://developers.facebook.com/apps 
config.Add("appId", "3955......."); 
config.Add("secret", "4c1d..............."); 
config.Add("fileUpload", true); //optional 
FacebookClient client = new FacebookClient(config); 
ulong facebookId = client.getUser(); //retrieve user id. if user is not added the app this value is 0 
client.getAccessToken() 

给你后访问令牌。

0

This page使我怀疑你的代码应该看起来更像是这样的:

dynamic result = fbClient.Get("oauth/access_token", new 
     { 
      client_id = fbClient.AppId, 
      client_secret = fbClient.AppSecret, 
      grant_type = "fb_exchange_token", 
      fb_exchange_token = accessToken 
     }); 

也许你的accessToken已超时什么的?