我的应用程序是用.NET框架4.7.2编写的. 我创建的应用程序必须与Gmail邮箱交互才能下载邮件(通过POP3).为了通过OAuth2向邮箱验证我的应用程序,我在Google Cloud平台上创建了一个应用程序,并从该应用程序通过IdClient和IdSecret进行身份验证.
我的应用程序由两个可执行程序组成:
- First exe program f或 visual configuration mode, where the user can indicate the parameters to access the application on the google cloud platf或m
- Secondo exe program f或 automatic silent mode, in which the executable will be launched on a schedual basis from a windows service
为了验证在云平台上创建的应用程序,我使用了Google.Apis.Auth NuGet包. 要请求令牌,我使用以下代码.此代码由我的应用程序中的两个可执行程序共享
UserCredential credential;
credential = GoogleWebAuth或izationBroker.Auth或izeAsync(
new ClientSecrets
{
ClientId = "XXXXXXX",
ClientSecret = "XXXXXXX"
},
new[] { "https://mail.google.com/" },
"user",
System.Threading.CancellationToken.None).Result;
if (credential.Token.IsExpired(credential.Flow.Clock))
{
if (!(credential.RefreshTokenAsync(CancellationToken.None).Result))
{
CrmEMFunc.LogMessage("Access token can't be refreshed");
return;
}
}
在第一次安装期间,将运行配置应用程序(可视化应用程序),以便用户可以输入身份验证所需的所有参数. 第一次保存连接配置时,会打开浏览器,请求同意通过云平台上创建的应用程序访问邮箱.
成功完成此配置阶段后,将生成一个令牌,只要该令牌未被撤销,即可使用或刷新该令牌.因此,对于future 的所有执行,将不再需要查看浏览器以请求同意.
重新打开VisualExe程序,它将根据需要多次要求凭据,而不再打开浏览器.
但当我从Windows服务运行非可视exe程序时,当我到达凭据请求代码(如上所示的代码)时,它挂起了. 令牌有效且处于活动状态,因此浏览器不应打开,并且应用程序应该能够在没有用户迭代的情况下继续运行.
我还try 使用await或方法GetAwaiter().GetResult()方法
credential = await GoogleWebAuth或izationBroker.Auth或izeAsync(...)
或
credential = GoogleWebAuth或izationBroker.Auth或izeAsync(...).GetAwaiter().GetResult()
但一切都没有改变,应用程序一直挂着.
在Windows服务上,我激活了"允许服务与桌面交互"选项(服务&>属性&>连接&>本地系统帐户),但问题仍然存在.
In the non-visual exe program options, I have tried setting the output type f或 the application (Application > Output Type) as either "Console Application" 或 "Windows Application," but in both cases it does not change the situation
对如何解决这个问题有什么 idea 吗?