0

我正在尝试对他的 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);

我用 aHttpWebRequest来做 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==true,则必须提供请求正文。通过在 [Begin]GetResponse 之前调用 [Begin]GetRequestStream 来执行此操作。

然后,我将 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/

关于其他方法的任何建议,或者如何纠正我所拥有的?

4

3 回答 3

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

注意 URL 编码!

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

于 2013-11-06T17:42:24.727 回答
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);
}

问题在于传递的 url,我在https://accounts.google.com/o/oauth2/token?不应该传递的时候传递。

于 2013-11-07T09:44:45.220 回答
0

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

httpWReq.ContentLength = 0;
于 2013-11-05T19:27:56.553 回答