C# 谷歌硬盘OAuth连接问题
在我的程序中,我使用以下行启动浏览器窗口,让用户登录并授权权限:C# 谷歌硬盘OAuth连接问题,c#,visual-studio-2010,asynchronous,google-drive-api,C#,Visual Studio 2010,Asynchronous,Google Drive Api,在我的程序中,我使用以下行启动浏览器窗口,让用户登录并授权权限: try { using (var stream = GenerateClientSecretsStream(this._client_id, this._client_secret)) { GoogleWebAuthorizationBroker.Folder = "Tasks.Auth.Store";
try
{
using (var stream = GenerateClientSecretsStream(this._client_id, this._client_secret))
{
GoogleWebAuthorizationBroker.Folder = "Tasks.Auth.Store";
StoredCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"user",
CancellationToken.None,
new SavedDataStore()).Result;
}
}
catch (AggregateException ae)
{
MessageBox.Show("Error when connecting to Google Drive");
return;
}
如果您停留在浏览器中并按照预期的步骤操作,则此操作正常,但在测试后,如果您关闭该窗口并返回到程序,它将被冻结,等待从浏览器返回输入。我试着研究async/await的内容,但我使用的是VS2010,所以看起来这些选项对我来说不可用
我已经查找了VS2010的异步CTP,但显然不建议将其用于发布版本。在这种情况下,如何防止程序锁定?当您访问异步方法的Result属性时,基本上是同步执行它,因为它实际上与使用Wait()相同 您应该做的是等待您的方法,该方法将把控制权交还给调用者,直到该方法完成执行: 编辑 忘记提及,如果您的方法同步运行或返回void,则应更改为以下签名: 异步任务MyFooMethod
try
{
using (var stream = GenerateClientSecretsStream(this._client_id, this._client_secret))
{
GoogleWebAuthorizationBroker.Folder = "Tasks.Auth.Store";
await StoredCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"user",
CancellationToken.None,
new SavedDataStore());
}
}
catch (AggregateException ae)
{
MessageBox.Show("Error when connecting to Google Drive");
return;
}
由于我使用的是VS 2010,所以我无法使用Wait。你找到答案了吗?Bryan?我自己也有同样的问题,我也被困在同样的场景中……可能它只适用于VS2012@AbdulRahmanAnsari我相信这就是我最终所做的,使这项工作成功