用于access_token的Youtube API C#OAuth令牌交换返回无效请求

时间:2013-11-05 19:06:20

标签: c# youtube-api oauth-2.0 google-oauth

我正在尝试对其Google帐户中的用户进行身份验证,以访问和修改其Youtube数据。 我设法获得从用户登录和条款接受返回的令牌,但是当我执行POST以交换用户access_token的令牌时,它会在Json文件上返回“无效请求”。

以下是我显示登录页面的方式:

string url = string.Format("https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXX&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://gdata.youtube.com&response_type=code&access_type=offline");
WBrowser.Navigate(new Uri(url).AbsoluteUri);

我使用HttpWebRequest来发布POST

string url = "https://accounts.google.com/o/oauth2/token?code=XXXXXX&client_id=XXXXXXXXX&client_secret=XXXXXXXXX&redirect_uri=http://localhost/oauth2callback&grant_type=authorization_code";

HttpWebRequest httpWReq =  (HttpWebRequest)WebRequest.Create(url);

byte[] byteArray = Encoding.UTF8.GetBytes(url);

httpWReq.Method = "POST";
httpWReq.Host = "accounts.google.com";
httpWReq.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
httpWReq.ContentLength = byteArray.Length;

但它在这一行失败了:

HttpWebResponse myHttpWebResponse = (HttpWebResponse)httpWReq.GetResponse();

出现以下错误:

  

如果设置ContentLength> 0或,则必须提供请求正文   SendChunked ==真。通过之前调用[Begin] GetRequestStream来完成此操作   [开始]的GetResponse。

然后我将我的POST请求更改为TcpClient,如下所示:

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);
}

但是在JSon文件中返回以下内容:

{
  "error" : "invalid_request"
}

我正在使用Youtube API:https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Installed_Applications_Flow

您可以在此处测试OAuth:https://developers.google.com/oauthplayground/

有关其他方法的建议,或者如何更正我的建议?

3 个答案:

答案 0 :(得分:2)

你正在使用ContentType =“application / x-www-form-urlencoded; charset = utf-8”进行POST;和内容长度,但然后传递URL中的参数。

来自oauth游乐场的简单复制/粘贴显示您的帖子应该是什么样的......

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-length: 250
content-type: application/x-www-form-urlencoded
user-agent: google-oauth-playground

code=4%2FfTu47TyLyXFg6eM2TcKv_ikQ1JJ1.kmu1bd6T8vsVXE-sT2ZLcbQH1fhchAI&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=407408718192.apps.googleusercontent.com&scope=&client_secret=************&grant_type=authorization_code

请注意网址编码!

此外,我上次尝试时,不允许使用 localhost 作为重定向网址。尝试将localhost映射到(例如)devserver.mydomain.com在etc / hosts文件中,并将其指定为重定向URL。确保您也在API控制台中添加它。

答案 1 :(得分:0)

Uri字符串和HTTP标头不是内容的一部分。因此,只需将ContentLength指定为零:

httpWReq.ContentLength = 0;

答案 2 :(得分:0)

我终于设法解决了这个问题。我最终使用了TcpClient版本,因为它是我第一次成功运行。所以最终的代码是这样的:

string _clientID = HttpUtility.UrlEncode("XXXXXXXXXXX.apps.googleusercontent.com");
string _clientSecret = HttpUtility.UrlEncode("XXXXXXXXXX");
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=authorization_code";

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);
}

问题在于传递的网址,我正在通过https://accounts.google.com/o/oauth2/token?,而我不应该这样做。