我在javascript的帮助下获得了授权代码。现在在服务器上,我需要交换刷新令牌。这是我的代码。
byte[] buffer = Encoding.ASCII.GetBytes("code=" + "....."
+ "&client_id=.....&client_secret=...&redirect_uri=https%3A%2F%2Fmysite.com%2Fportal%2F&grant_type=authorization_code");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;
Stream strm = req.GetRequestStream();
strm.Write(buffer, 0, buffer.Length);
strm.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
它引发异常Bad request
,而内部异常为null。
更新:
我在ASP.NET MVC 4应用程序中使用它。由于它是异步操作,因此我切换到Visual Studio2013。我使用以下代码修改了代码await
:
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] {
GmailService.Scope.GmailCompose, GmailService.Scope.GmailModify, GmailService.Scope.GmailReadonly
},
"user",
CancellationToken.None
) ;
在此语句中,它挂起了浏览器(类似于此问题)。我尝试放置日志,没有例外,也没有进一步执行。
Google身份验证的第一步是响应类型代码。它是一个HTTP GET,基本上只是打开一个新的浏览器窗口。用户接受您的身份验证后,您将获得一个身份验证码,然后必须将其返回到您的应用程序。它还显示在浏览器标题中。
https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code
有关这三个步骤的完整说明,请参见此处。Google 3 Legged oauth2
我不确定我是否理解您为什么这样做。使用Google .net客户端库要容易得多。NuGet Google客户端库我不确定您打算访问哪个API,但可以使用nuget将其谷歌浏览,您应该找到要安装的正确库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句