2012-03-19 89 views
1

我被困在最近5個小時的Facebook身份驗證,我沒有很多想法,爲什麼我得到錯誤:400壞請求。 我在網上找到了一個用戶獲得授權的示例應用程序,然後繼續使用應用程序,我在本地運行相同的應用程序,並且能夠正確運行它並獲取訪問令牌。 這裏是獲得令牌的方法:facebook登錄在asp.net 400不好的請求

/// <summary> 
     /// Get the authorisation token 
     /// </summary> 
     /// <param name="code"></param> 
     /// <returns></returns> 
     public Dictionary<string, string> GetAccessToken(string code, string scope,string redirectUrl) 
     { 
      Dictionary<string, string> tokens = new Dictionary<string, string>(); 
      string clientId = FacebookContext.Current.AppId;    
      string clientSecret = FacebookContext.Current.AppSecret; 
      string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}", 
          clientId, redirectUrl, clientSecret, code, scope); 
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
      { 
       StreamReader reader = new StreamReader(response.GetResponseStream()); 
       string retVal = reader.ReadToEnd(); 

       foreach (string token in retVal.Split('&')) 
       { 
        tokens.Add(token.Substring(0, token.IndexOf("=")), 
         token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1)); 
       }     
      } 
      return tokens; 
     } 

當響應嘗試從請求對象的響應時出現的問題:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 

與樣本應用程序,我沒有任何問題,但在應用程序中,我試圖使用代碼,我得到了400錯誤的請求錯誤。 url參數看起來非常相似,這就是爲什麼我不知道我做錯了什麼。 這裏是URL的示例應用程序格式:

https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=http://localhost:5000/fbregcallback.aspx&client_secret=982161272e8adc3af17952dc0f70b67e&code=AQBSvKfdc3ynTWeJTVSq7Y2JRV52YNCHPV5nAluxVf3_Ger68NJZeWScbKg1ttMfVOuXrOTfbvgq6o_bXIemnlx2yH-eu2vDvs-94EDEPxHFPUTQ4UjnSgxKAZjs6h-APxqHrRz5gvlafYb89uIoGhBuu-sIPrvIThBK-BmFRuytrj2fHYSU9y_xbFIjAYNUw_U&scope=user_birthday,publish_stream,create_event,user_photos 

我的應用程序的URL格式:

https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=http://localhost:47736/Default.aspx&client_secret=982161272e8adc3af17952dc0f70b67e&code=AQBd_IYcBLy-UfV68PDH8gWFOud5eLzVt8ZukSVfo35qSLw7jn5yrpw-zqr4WhVigv0G1tVtEVIj2fnvBAzmWU3SlOtZgCC-0P1sBRQ999JP60Qe4s0UT1c0Z1cupMW8qNdDfuBhzxty9581gTISPq9xYF1SEifX60kDUc1bd7FuUGsGyGBgtfzO8PdNZoGrq6Y&scope=user_birthday,publish_stream,create_event,user_photos 

每一個意見會受到歡迎,因爲這個問題其實就是我的神經在這一刻。 非常感謝,Laziale

+0

如果您在本地計算機上工作,則大多數fb應用程序例如fb登錄無法工作。這可能是問題。 – irfanmcsd 2012-03-19 13:03:08

+0

@MuhammadIrfan其他示例在本地計算機上運行良好,我可以獲取訪問令牌,並且它是相同的url – Laziale 2012-03-19 13:07:25

回答

1

Facebook往往不喜歡localhost的URL。您可以在主機文件(例如fbdev.com)中爲虛假域創建條目,並在開發人員控制檯中將該域(包含端口)設置爲應用程序的URL和域,並將其設置爲應用程序的基本設置。

+0

您好,我使用ngrok在線連接我的服務器。它會起作用嗎? – toha 2017-09-05 06:59:19