2011-04-02 114 views
1

我從facebook請求訪問令牌時遇到了一些問題。首先它工作。但現在它不再工作了,我不認爲我在特定的代碼行中改變了一些東西。當請求訪問facebook的facebook請求錯誤時出現錯誤的請求錯誤

下面是我如何做了它:

public static String requestAccesToken(string code) 
    { 
     //create the constructor with post type and few data 
     MyWebRequest myRequest = new MyWebRequest("https://graph.facebook.com/oauth/access_token?", "POST", "client_id=" + getAppID() + "&client_secret=" + getAppSecret() + "&code=" + code + "&redirect_uri=http://localhost:57689/search.aspx"); 

     string accessToken = myRequest.GetResponse().Split('&')[0]; 
     accessToken = accessToken.Split('=')[1]; 

     return accessToken; 
    } 
public static String getAppID() 
    { 
     return System.Configuration.ConfigurationManager.AppSettings["AppID"].ToString(); 
    } 

    public static String getAppSecret() 
    { 
     return System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString(); 
    } 

類:MyWebrequest:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Net; 
using System.IO; 
using System.Text; 

public class MyWebRequest 
{ 
    private WebRequest request; 
    private Stream dataStream; 

    private string status; 

    public String Status 
    { 
     get 
     { 
      return status; 
     } 
     set 
     { 
      status = value; 
     } 
    } 
    /* 
    * simply download the data of the web page 
    */ 
    public MyWebRequest(string url) 
    { 
     // Create a request using a URL that can receive a post. 

     request = WebRequest.Create(url); 
    } 

    public MyWebRequest(string url, string method) 
     : this(url) 
    { 

     if (method.Equals("GET") || method.Equals("POST")) 
     { 
      // Set the Method property of the request to POST. 
      request.Method = method; 
     } 
     else 
     { 
      throw new Exception("Invalid Method Type"); 
     } 
    } 

    public MyWebRequest(string url, string method, string data) 
     : this(url, method) 
    { 

     // Create POST data and convert it to a byte array. 
     string postData = data; 
     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/x-www-form-urlencoded"; 

     // Set the ContentLength property of the WebRequest. 
     request.ContentLength = byteArray.Length; 

     // Get the request stream. 
     dataStream = request.GetRequestStream(); 

     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 

     // Close the Stream object. 
     dataStream.Close(); 

    } 

    public string GetResponse() 
    { 
     // Get the original response. 
     WebResponse response = request.GetResponse(); //400 ERROR BAD REQUEST 

     this.Status = ((HttpWebResponse)response).StatusDescription; 

     // Get the stream containing all content returned by the requested server. 
     dataStream = response.GetResponseStream(); 

     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 

     // Read the content fully up to the end. 
     string responseFromServer = reader.ReadToEnd(); 

     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

     return responseFromServer; 
    } 

} 

這是哪裏的問題呢?我真的堅持這一個。

謝謝您的閱讀!

回答

4

昨天我有同樣的問題。我使用WebClient而不是WebRequest來修復它。我認爲Http error並出現因爲響應是使用gzip的編碼,因爲我是在REDIRECT_URI發送一些參數(我不知道如果是這樣的原因,但現在運轉):

  try 
      { 
       WebClient wc = new WebClient(); 

       wc.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip"); 

       //that solves the problem with GZIP partially: 
       wc.Proxy = null; 

       using (StreamReader reader = new StreamReader(wc.OpenRead(url))) 
       { 
        string conteudo = reader.ReadToEnd(); 

        ... 
+1

謝謝 - 經過幾個小時的努力弄清楚,並讓它在瀏覽器中工作,但沒有WebClient我知道標題與它有關。 添加標題並將代理設置爲null,如上面顯示的那樣修復了400錯誤。 :) – 2011-10-09 09:54:00