Oauth2:远程服务器返回错误:(400) Bad Request
Posted
技术标签:
【中文标题】Oauth2:远程服务器返回错误:(400) Bad Request【英文标题】:Oauth2: The remote server returned an error: (400) Bad Request 【发布时间】:2013-04-20 22:02:39 【问题描述】:我想在内部应用程序中实现 Google 的 Oauth2,并且正在使用 C# 控制台应用程序玩此处找到的演示:
https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs
我已将所有必要的 Google API 库添加到我的项目(核心客户端、YouTube、联系人……)并注册了我的应用程序……..
当我执行程序时,我可以从授权客户端取回访问代码等……我正在使用正确的客户端 ID、ClientSecret 密钥……。
我最终得到一个异常:远程服务器返回错误:(400) Bad Request。
从此方法抛出:
public static void GetAccessToken(OAuth2Parameters parameters)
OAuthBase.GetOAuth2AccessToken(parameters, OAuthBase.GetExchangeAccessCodeRequestBody(parameters));
调用者:
public static void GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody) Uri requestUri = new Uri(parameters.TokenUri); WebRequest 请求 = WebRequest.Create(requestUri); request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream outputStream = request.GetRequestStream();
StreamWriter w = new StreamWriter(outputStream);
w.Write(requestBody);
w.Flush();
w.Close();
WebResponse response = request.GetResponse();
string result = "";
if (response != null)
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
result = reader.ReadToEnd();
Dictionary<string, string> dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
if (dict.ContainsKey(OAuth2AccessToken))
parameters.AccessToken = dict[OAuth2AccessToken];
if (dict.ContainsKey(OAuth2RefreshToken))
parameters.RefreshToken = dict[OAuth2RefreshToken];
if (dict.ContainsKey(OAuth2TokenType))
parameters.TokenType = dict[OAuth2TokenType];
if (dict.ContainsKey(OAuth2ExpiresIn))
parameters.TokenExpiry = DateTime.Now.AddSeconds(int.Parse(dict[OAuth2ExpiresIn]));
有人可以解释一下可能导致以下错误的原因吗?
System.Net.WebException was unhandled
HResult=-2146233079
Message=The remote server returned an error: (400) Bad Request.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at Google.GData.Client.OAuthBase.GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody) in c \Development\lib\YouTube\libgoogle-data-mono-2.1.0.0\src\core\oauthbase.cs:line 435
at Google.GData.Client.OAuthUtil.GetAccessToken(OAuth2Parameters parameters) in c:\ \lib\YouTube\libgoogle-data-mono-2.1.0.0\src\core\oauthutil.cs:line 246
at GoogleAppsConsoleApplication.OAuth2Demo.Main(String[] args) in c:\Users\test\Documents\Visual Studio 2012\Projects\GoogleAppsConsoleApplication\GoogleAppsConsoleApplication\Program.cs:line 60
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
【问题讨论】:
【参考方案1】:您正在使用旧的 gdata 协议。我建议您使用Google APIs client library。 查看https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 了解如何使用 OAuth2 的详细信息。
【讨论】:
谢谢。我会在周末试试这个。你有 Oauth2 身份验证的 dotnet 示例吗?优先访问 YouTube 库。以上是关于Oauth2:远程服务器返回错误:(400) Bad Request的主要内容,如果未能解决你的问题,请参考以下文章
HttpWebRequest-远程服务器返回错误:(400) Bad Request