我正在尝试验证其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
进行开机自检
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 或 发送分块==真。 通过在之前调用 [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/
关于其他方法的任何建议,或者如何纠正我的问题?
你正在制作一个 POST,ContentType = "application/x-www-form-urlencoded;字符集=UTF-8";和内容长度,但随后在 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 编码!
另外,上次我尝试时,不允许使用本地主机作为重定向 URL。尝试将本地主机映射到(例如)您的 etc/hosts 文件中的 devserver.mydomain.com,并将其指定为重定向 URL。请确保在 API 控制台中添加此项。
Uri 字符串和 HTTP 标头不是内容的一部分。因此,只需将 ContentLength 分配给零:
httpWReq.ContentLength = 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?
时传递了。