2013-02-20 21 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已超時什麼的?