2

こんにちは、次のコードを使用して、C# SDK を使用して Facebook からアクセス トークンを取得しています。

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.

この問題を解決するには??

4

4 に答える 4

4

あなたのプロジェクトの種類は何ですか: WinFormsWPFASP.NET ?

WinFormsまたはWPFを使用している場合は、OAuth ログイン ダイアログと をリクエストしてaccess_tokenフォームのURLを取得し、URL から有効なものを抽出する必要があります。Browser Controlreturn_type=tokenaccess_token

それ以外の場合、ASP.NETを使用して Web アプリケーションで作業している場合は、ユーザーを OAuth ダイアログ ログイン ページにリダイレクトする必要があります。その後、Facebook が URL のコードを使用してリダイレクトしますQueryStringHTTPRequestFacebook にアクセスして、有効な を取得します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);
}
于 2013-02-20T15:28:37.273 に答える
0

このページは、あなたのコードが次のように見えるべきかどうか疑問に思います:

   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 はタイムアウトか何かですか?

于 2013-04-12T19:34:50.750 に答える
0

redirect_uriを求めたときと同じものを用意する必要がありますcode
また、Facebook の「Facebook ログインを使用した Web サイト」セクションで構成したサイト URL の末尾には、末尾のスラッシュ「/」が必要です。
完全なチュートリアルは次のとおりです: C# SDKの使用

于 2013-02-20T11:00:23.657 に答える
-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()

アクセストークンを発行します。

于 2013-03-12T14:38:07.623 に答える