【问题标题】:Exception in getting Facebook API User Access Token and C#获取 Facebook API 用户访问令牌和 C# 的异常
【发布时间】:2011-11-17 05:38:03
【问题描述】:

我收到以下错误:

(OAuthException) 不应将 client_secret 传递给 /oauth/access_token/

当我调用 Facebook API 以获取“用户访问令牌”以进行进一步查询时。我的代码很简单:

string appId = "99999999"; // Long number  - Given by FB in my application page 
string appSecret = "98907kjlhkh908098"; // // Long string- Given by FB in my application page 
string code = "089789uokjj"; // Access Code in response to my first FB call - It is in query string of response
var fb = new FacebookClient ( appId , appSecret );

var parameters = new Dictionary<string , object>
{
    { "client_id", appId },
    {"redirect_uri" , "http://localhost:49773/Sample/Default.aspx"},
    { "client_secret", appSecret },
    { "code", code }
};

fb.Post( "https://graph.facebook.com/oauth/access_token/" , parameters );

我如何传递Client_Secret?没有它,我将无法继续,并且再次遇到异常!

【问题讨论】:

    标签: c# facebook facebook-graph-api facebook-c#-sdk


    【解决方案1】:

    用于服务器端流程的 ASP.NET 小教程(我使用的是 FB C# SDK 版本 6):

    1)在登录页面创建登录按钮并绑定onclick事件:

    var loginWindowPopup = null;
    var loginWindowTimer = null;
    
    $(function ()
    {
        $('#login_with_facebook').click(function ()
        {
            var popupWidth = 640;
            var popupHeight = 337;
            var xPosition = ($(window).width() - popupWidth) / 2;
            var yPosition = ($(window).height() - popupHeight) / 2;
    
            loginWindowPopup = window.open('/AuthSocialUser.aspx?facebookAuth=true', 
              'FacebookLoginWindow', 
              'location=1,scrollbars=1,menubar=0,status=0,toolbar=0' +
              ',width=' + popupWidth +
              ',height=' + popupHeight +
              ',left=' + xPosition +
              ',top=' + yPosition);
    
            if (loginWindowTimer == null)
            {
                loginWindowTimer = setInterval(CheckLogonWindowClose, 1000);
            }
        }
    );
    
    function CheckLogonWindowClose()
    {
        if (loginWindowPopup.closed)
        {
            clearInterval(loginWindowTimer);
            location.reload();
        }
    };
    

    2) 在 AuthSocialUser.aspx 弹出窗口中:

    if (this.Request.QueryString["facebookAuth"] == "true")
    {
        var parameters = new Dictionary<string,object>();
        parameters["client_id"] = "...";
        // parameters["scope"] = "email";
    
        string state = Guid.NewGuid().ToString();
        parameters["state"] = state;
        this.Session.Add("state", state); //CSRF protection
    
        parameters["redirect_uri"] =    
          this.Request.Url.AbsoluteUri.Replace("facebookAuth=true", "facebookAuth=false");
    
        parameters["response_type"] = "code"; // code can be exchanged for an access token
    
        parameters["display"] = "popup";
    
        this.Response.Redirect(new FacebookClient().GetLoginUrl(parameters).AbsoluteUri);
    }
    else
    {
        string code = this.Request.QueryString["code"];
        string state = this.Request.QueryString["state"];
        string currentState = (this.Session["state"] != null ? 
            this.Session["state"].ToString() : null);
    
        if (string.IsNullOrWhiteSpace(code) == true)
        {
            // set info in session: app not authorized & inject close window JS script
            return;
        }
    
        if (string.IsNullOrWhiteSpace(state) == true || 
            string.IsNullOrWhiteSpace(currentState) == true)
        {
            // session state expired & inject close window JS script
            return;
        }
    
        if (state != currentState)
        {
            throw new ArgumentException("State does not match (CSRF?)");
        }
    
        //// get access token
        var fb = new FacebookClient();
    
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("client_id", "...");
        parameters.Add("redirect_uri", "https://127.0.0.1
           /AuthSocialUser.aspx?facebookAuth=false");
        parameters.Add("client_secret", "...");
        parameters.Add("code", code);
    
        result = fb.Get("/oauth/access_token", parameters);
    
        string accessToken = result["access_token"];
    
        // use token in next requests, insert status to session state 
        // & inject close window JS script - simple: window.close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多