如何为我的 asp.net Web 应用程序获取 Google Api OAuth 2 刷新令牌?
How do I get a Google Api OAuth 2 refresh token for my asp.net web application?
我正在尝试在我的 Asp.net 应用程序 (C#) 中使用 OAuth 2。问题是我需要使用共享 google 帐户。为此,我的计划是使用令牌、到期日期和刷新令牌作为身份验证的种子,然后在需要身份验证时检查到期日期并使用刷新令牌。
我一直用于身份验证的示例如下所示:
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
并且似乎不包含带有刷新标记的对象。
如何获取刷新令牌和到期日期?
解决方案是执行 post 操作并手动解析结果,而不是使用任何 Google classes。
string gurl = "code=" + code + "&client_id=" + client_id +
"&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=" + grant_type;
string url = "https://www.googleapis.com/oauth2/v3/token";
// creates the post data for the POST request
string postData = (gurl);
// create the POST request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Host ="www.googleapis.com";
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
// POST the data
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(postData);
}
//This actually does the request and gets the response back
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
string googleAuth;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
//dumps the HTML from the response into a string variable
googleAuth = responseReader.ReadToEnd();
}
从那里我主要需要解析 googleAuth 字符串以获取令牌、刷新令牌和有效期。我一直期待在 Google classes 中有一个解决方案来解决一个非常普遍的请求,但显然我会创建自己的 class.
我正在尝试在我的 Asp.net 应用程序 (C#) 中使用 OAuth 2。问题是我需要使用共享 google 帐户。为此,我的计划是使用令牌、到期日期和刷新令牌作为身份验证的种子,然后在需要身份验证时检查到期日期并使用刷新令牌。
我一直用于身份验证的示例如下所示:
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
并且似乎不包含带有刷新标记的对象。
如何获取刷新令牌和到期日期?
解决方案是执行 post 操作并手动解析结果,而不是使用任何 Google classes。
string gurl = "code=" + code + "&client_id=" + client_id +
"&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=" + grant_type;
string url = "https://www.googleapis.com/oauth2/v3/token";
// creates the post data for the POST request
string postData = (gurl);
// create the POST request
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Host ="www.googleapis.com";
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
// POST the data
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(postData);
}
//This actually does the request and gets the response back
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
string googleAuth;
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
//dumps the HTML from the response into a string variable
googleAuth = responseReader.ReadToEnd();
}
从那里我主要需要解析 googleAuth 字符串以获取令牌、刷新令牌和有效期。我一直期待在 Google classes 中有一个解决方案来解决一个非常普遍的请求,但显然我会创建自己的 class.