【发布时间】:2021-02-11 18:59:41
【问题描述】:
我有一个由服务器处理的应用程序来请求大量电子邮件(10-30k 电子邮件),但是当通过 API 使用 google 进行身份验证时,该窗口在服务器上打开,而不是在客户端上打开。 为了简化请求是在本地发出的(所以在服务器上)。
那么我怎样才能在服务器上发出请求并在客户端上获取 google 的身份验证窗口?
这是我的代码:
Public Bool Connect()
{
MemoryStream memoryStream;
GoogleClientSecrets googleClientSecrets;
string credentialPath;
List<string> scopes = new List<string>();
UserCredential userCredential;
try
{
memoryStream = new MemoryStream(credentialKeyFile);
googleClientSecrets = GoogleClientSecrets.Load(memoryStream);
}
catch (Exception exception)
{
return false;
}
try
{
credentialPath = Path.Combine(credentialsPath, account);
scopes.Add(GmailService.Scope.MailGoogleCom);
userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
googleClientSecrets.Secrets,
scopes.ToArray(),
account + ".json",
CancellationToken.None,
new FileDataStore(credentialPath, true)
).Result;
}
catch (Exception exception)
{
return false;
}
if (userCredential == null)
{
GlobalMethods.AddLog("L'authentification à l'API Google a échoué.");
return false;
}
try
{
accountGmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = userCredential
});
}
catch (Exception exception)
{
return false;
}
if (accountGmailService == null)
{
return false;
}
return true;
}
【问题讨论】:
-
这是一个 asp.net 应用程序吗?不太清楚
-
不是 ASP.net,而是 C# 应用程序
-
如果它是一个应用程序,那么用户需要将它安装在他们的机器上,然后您就可以使用您拥有的代码。如果你想托管它,那么你应该使用 asp .net 并将其作为网络应用程序发布。
-
是的,昨天的问题已被删除。在我有机会向他解释问题是什么之前。即使在 OP 编辑了他的问题并添加了我要求的代码之后。 stackoverflow.com/q/64573922/1841839我不知道他为什么一直在这个问题上得到-1
-
@DaImTo 同意这没有帮助,似乎管理员可能没有真正正确地阅读它。不幸的是,我看到这种情况经常发生。但它仍然可以重新打开。不管怎样,现在已经完成了,这个问题照原样很好。
标签: c# .net google-oauth gmail-api google-api-dotnet-client