的Youtube API C#OAuth认证的access_token令牌交换返回无效请求 [英] Youtube API C# OAuth token exchange for access_token returns invalid request

查看:279
本文介绍了的Youtube API C#OAuth认证的access_token令牌交换返回无效请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在他的谷歌帐户的用户访问和修改他的YouTube的数据进行验证。
我设法从用户​​登录和条件接受返回的道理,但是当我做了POST交换令牌用户的access_token它返回的JSON文件是无效的请求。

I'm trying to authenticate a user in his Google Account to access and modify his Youtube data. I managed to get the token returned from the user login and terms acceptance, but when I do the POST to exchange the token for a user access_token it returns an "invalid request" on the Json file.

下面就是我如何显示登录页面:

Here's how I show the login page:

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;



但是,在这条线失败:

But it fails on this line:

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

通过以下错误:

如果你设置CONTENTLENGTH> 0或
SendChunked == true您必须提供一个请求主体。通过调用做到这一点[开始] GetRequestStream前
[开始]的GetResponse。

You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse.

然后我改变了我的POST请求TcpClient的像这样的:

I then changed my POST request to TcpClient like this:

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文件如下:

But returns the following in the JSon file:

{
  "error" : "invalid_request"
}

我follwoing Youtube的API: https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Installed_Applications_Flow

I'm follwoing the Youtube API: https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Installed_Applications_Flow

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

And you can test OAuth here: https://developers.google.com/oauthplayground/

任何建议其他的方法可以做到这一点,或如何纠正我有什么?

Any suggestions on other ways to do this, or how to correct what I have?

推荐答案

我终于设法解决这个问题。我结束了使用TcpClient的版本,因为这是第一次我设法得到工作。所以,最终的代码是这样的:

I finally managed to solve this. I ended up using the TcpClient version, since it was the first I managed to get working. So the final code is something like this:

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?时,我不应该有。

The problem was with the passing url, I was passing the https://accounts.google.com/o/oauth2/token? when I shouldn't have.

这篇关于的Youtube API C#OAuth认证的access_token令牌交换返回无效请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆