2
獲取訪問令牌和刷新令牌我藉助javascript獲得了授權代碼。現在在服務器上,我需要刷新令牌來交換。這是我的代碼。Google Oauth 2從授權碼
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問題)。我試着把日誌,沒有例外,沒有進一步的執行。
謝謝。只有在您的文章(http://www.daimto.com/google-oauth2-csharp/)的幫助下,我才能從服務器端完成此操作。我有疑問...我們可以控制用戶授權是如何完成的嗎?如顯示一個彈出窗口而不是全新的瀏覽器選項卡?或者它不在我們的控制之下? – benjamin54
不在我們的控制之下 – DaImTo
有沒有辦法獲得離線訪問令牌?我需要保留刷新標記並在後面使用它。 – benjamin54