YoutubeAPIC#OAuthtokenexchangeforaccess_token返回无效请求认证访问和修改其Youtube数据。我设法从用户??登录和接受条款中获取返回的令牌,但是当我执行POST以将令牌交换为用户access_token时,它在Json文件中返回“无效请求”。这是我显示登录页面的方式:stringurl=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(新Uri(url).AbsoluteUri);我使用HttpWebRequest来POSTstringurl="https://accounts.google.com/o/oauth2/token?code=XXXXXX&client_id=XXXXXXXXX&client_secret=XXXXXXXX&redirect_uri=http://localhost/oauth2callback&grant_type=authorization_code";HttpWebRequesthttpWReq=(HttpWebRequest)WebRequest.Create(url);byte[]courl)httpWReq.Method="POST";httpWReq.Host="accounts.google.com";httpWReq.ContentType="application/x-www-form-urlencoded;charset=utf-8";httpWReq.ContentLength=byteArray.Length;但它失败了:HttpWebResponsemyHttpWebResponse=(HttpWebResponse)httpWReq.GetResponse();出现以下错误:如果设置了ContentLength>0或SendChunked==true,则必须提供请求文本。通过在[Begin]GetResponse之前调用[Begin]GetRequestStream来执行此操作。然后我将我的POST请求更改为TcpClient,如下所示:TcpClientclient=newTcpClient("accounts.google.com",443);流netStream=client.GetStream();SslStreamsslStream=newSslStream(netStream);sslStream.AuthenticateAsClient("accounts.google.com");{byte[]contentAsBytes=Encoding.ASCII.GetBytes(url.ToString());StringBuildermsg=newStringBuilder();msg.AppendLine("POST/o/oauth2/tokenHTTP/1.1");msg.AppendLine("主机:accounts.google.com");msg.AppendLine("Content-Type:application/x-www-form-urlencoded");msg.AppendLine("Content-Length:"+contentAsBytes.Length.ToString());msg.AppendLine("");Debug.WriteLine("请求");Debug.WriteLine(msg.ToString());Debug.WriteLine(url.ToString());byte[]headerAsBytes=Encoding.ASCII.GetBytes(msg.ToString());sslStream.Write(headerAsBytes);sslStream.Write(contentAsBytes);}Debug.WriteLine("响应");StreamReader阅读器=newStreamReader(sslStream);while(true){//将响应逐行打印到debug流以供检查。字符串行=reader.ReadLine();如果(行==空)中断;调试.WriteLine(线);但在JSON文件中返回以下内容:{“error”:“invalid_request”}我使用YoutubeAPI:https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Installed_Applications_Flow您可以在此处测试OAuth:https://developers.google.com/oauthplayground/对其他方法的建议,或如何纠正我?您正在使用ContentType="application/x-www-form-urlencoded;charset=utf-8";进行POST;和内容长度,然后在URL中传递参数来自oauth游乐场的简单复制/粘贴显示了您的帖子应该是什么样子......POST/o/oauth2/tokenHTTP/1.1Host:accounts.google.comContent-length:250content-type:application/x-www-form-urlencodeduser-agent:google-oauth-playgroundcode=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。尝试将localhost映射到(例如)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);stringurl="code="+_code+"&client_id="+_clientID+"&client_secret="+_clientSecret+"&redirect_uri="+_redirectUri+"&grant_type=authorization_code";TcpClient客户端=newTcpClient("accounts.google.com",443);流netStream=client.GetStream();SslStreamsslStream=newSslStream(netStream);sslStream.AuthenticateAsClient("accounts.google.com");{byte[]contentAsBytes=Encoding.ASCII.GetBytes(url.ToString());StringBuildermsg=newStringBuilder();msg.AppendLine("POST/o/oauth2/tokenHTTP/1.1");msg.AppendLine("主机:accounts.google.com");msg.AppendLine("Content-Type:application/x-www-form-urlencoded");msg.AppendLine("内容长度:"+contentAsBytes.Length.ToString());msg.AppendLine("");Debug.WriteLine("请求");Debug.WriteLine(msg.ToString());Debug.WriteLine(url.ToString());byte[]headerAsBytes=Encoding.ASCII.GetBytes(msg.ToString());sslStream.Write(headerAsBytes);sslStream.Write(contentAsBytes);}Debug.WriteLine("响应");StreamReader阅读器=newStreamReader(sslStream);while(true){//将响应逐行打印到调试流以供检查。字符串行=reader.ReadLine();如果(行==空)中断;调试.WriteLine(线);我正在传递https://accounts.google.com/o/oauth2/token?WhenshouldInot以上为C#学习教程:YoutubeAPIC#OAuthtokenexchangeforaccess_tokenreturnsinvalidrequest分享全文,如果对你有用,需要了解更多C#学习教程,望采纳更多的关注它。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
