【发布时间】:2015-02-24 20:18:51
【问题描述】:
我在 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,内部异常为空。
更新:
我在 ASP.NET MVC 4 应用程序中使用它。由于是异步操作,所以我切换到Visual Studio 2013。我修改了代码await:
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] {
GmailService.Scope.GmailCompose, GmailService.Scope.GmailModify, GmailService.Scope.GmailReadonly
},
"user",
CancellationToken.None
) ;
在此语句中,它会挂起浏览器(类似于this 问题)。我试过放日志,没有异常,也没有进一步执行。
【问题讨论】:
标签: javascript c# oauth oauth-2.0 google-oauth