Google api Google AUTH API应用程序类型,它有多重要?
我一直在使用谷歌提供的.NET库来修补身份验证的东西 我们同时拥有桌面和web应用程序端,我们想要实现的是在桌面或web端进行一次身份验证,并存储刷新令牌,并在web端和桌面端重复使用它 因此,情况是这样的,在桌面端,当没有保存的现有AccessToken和RefreshToken时,我们将要求用户通过以下代码进行身份验证:Google api Google AUTH API应用程序类型,它有多重要?,google-api,google-api-dotnet-client,gmail-api,google-authentication,Google Api,Google Api Dotnet Client,Gmail Api,Google Authentication,我一直在使用谷歌提供的.NET库来修补身份验证的东西 我们同时拥有桌面和web应用程序端,我们想要实现的是在桌面或web端进行一次身份验证,并存储刷新令牌,并在web端和桌面端重复使用它 因此,情况是这样的,在桌面端,当没有保存的现有AccessToken和RefreshToken时,我们将要求用户通过以下代码进行身份验证: using (var stream = new FileStream("client_secrets_desktop.json", FileMode
using (var stream = new FileStream("client_secrets_desktop.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailCompose },
"someemail@gmail.com", CancellationToken.None);
}
在这种情况下,客户端ID和密码属于已安装应用程序的应用程序类型
在web应用程序端,如果还没有刷新令牌,那么我将使用DotNetOpenAuth触发身份验证,下面是代码片段:
const string clientID = "someclientid";
const string clientSecret = "somesecret";
const string redirectUri = "http://localhost/Home/oauth2callback";
AuthorizationServerDescription server = new AuthorizationServerDescription
{
AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
ProtocolVersion = ProtocolVersion.V20
};
public ActionResult AuthenticateMe()
{
List<string> scope = new List<string>
{
GmailService.Scope.GmailCompose,
GmailService.Scope.GmailReadonly,
GmailService.Scope.GmailModify
};
WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);
// Here redirect to authorization site occurs
OutgoingWebResponse response = consumer.PrepareRequestUserAuthorization(
scope, new Uri(redirectUri));
response.Headers["Location"] += "&access_type=offline&approval_prompt=force";
return response.AsActionResult();
}
public void oauth2callback()
{
WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);
consumer.ClientCredentialApplicator =
ClientCredentialApplicator.PostParameter(clientSecret);
IAuthorizationState grantedAccess = consumer.ProcessUserAuthorization(null);
string accessToken = grantedAccess.AccessToken;
}
我注意到的是,为了能够使用刷新令牌从桌面或web端刷新,需要通过相同的客户端ID/密码组合生成刷新令牌。我已经对它进行了测试,如果我们使用已安装的应用程序作为桌面和web的客户端ID的应用程序类型,这似乎很好,但我想我的问题是,这些应用程序类型是客户端ID的,它们有这么大的关系吗
我这样做有什么不对吗
提前感谢此网站并非用于代码审查。你的代码不同于我通常的做法,但如果它能工作,我认为它很好。只要客户端id、secret和refreshtoken匹配,您就可以在web和本机应用程序上使用它。为什么它们不同是一个很好的问题。
UserCredential uc = new UserCredential(flow, "someemail@gmail.com", new TokenResponse()
{
AccessToken = "lastaccesstoken",
TokenType = "Bearer",
RefreshToken = "supersecretrefreshtoken"
});
var refreshState = await uc.RefreshTokenAsync(CancellationToken.None);
var svc = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = uc,
ApplicationName = "Gmail Test",
});