2014-01-29 65 views
0

我不明白這個函數是什麼錯誤來獲得我的authkey? 我只會得到這個名爲「invalid_request」的錯誤,有沒有人有任何想法? 它似乎不理解我的要求?Oauth2 YouTube的「錯誤」:「invalid_request」

// Innk

public partial class OAuth : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 

    { 

string _clientID = HttpUtility.UrlEncode("***********"); 

     string _clientSecret = HttpUtility.UrlEncode("******"); 

     string _redirectUri = "urn:ietf:wg:oauth:2.0:oob"; 

     string _code = HttpUtility.UrlEncode("token"); 

     string url = "code=" + _code + "&client_id=" + _clientID + "&client_secret=" + _clientSecret + "&redirect_uri=" + _redirectUri + "&grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer"; 

     TcpClient client = new TcpClient("accounts.google.com", 443); 
     Stream netStream = client.GetStream(); 
     SslStream sslStream = new SslStream(netStream); 
     sslStream.AuthenticateAsClient("accounts.google.com"); 
     { 
      byte[] contentAsBytes = Encoding.ASCII.GetBytes(url.ToString()); 
      StringBuilder msg = new StringBuilder(); 
      msg.AppendLine("POST /o/oauth2/token HTTP/1.1"); 
      msg.AppendLine("Host: accounts.google.com"); 
      msg.AppendLine("Content-Type: application/x-www-form-urlencoded"); 
      msg.AppendLine("Content-Length: " + contentAsBytes.Length.ToString()); 
      msg.AppendLine(""); 
      Debug.WriteLine("Request"); 
      Debug.WriteLine(msg.ToString()); 
      Debug.WriteLine(url.ToString()); 
      byte[] headerAsBytes = Encoding.ASCII.GetBytes(msg.ToString()); 
      sslStream.Write(headerAsBytes); 
      sslStream.Write(contentAsBytes); 
     } 
     Debug.WriteLine("Response"); 
     StreamReader reader = new StreamReader(sslStream); 
     while (true) 
     { // Print the response line by line to the debug stream for inspection. 
      string line = reader.ReadLine(); 
      if (line == null) 
       break; 
      Debug.WriteLine(line); 
     } 

    } 
} 

回答

0

我會強烈建議任何人誰是做.NET開發,並希望訪問YouTube的API使用official client library

如果您不希望將客戶端庫用於所有操作,至少我建議您將它用於handle OAuth 2,然後使用它生成的令牌在請求的Authorization標頭中滾動自己。

相關問題