[英]Google Drive OAuth connect issue
在我的程序中,我使用以下行來啟動瀏覽器窗口,以使用戶登錄並授權權限:
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;
}
如果您留在瀏覽器中並按照預期的步驟進行,則此方法效果很好,但是在測試是否關閉了該窗口並返回到程序后,它將凍結,等待瀏覽器返回輸入。 我嘗試研究異步/等待的內容,但正在使用VS 2010,因此這些選項對我來說不可用。
我已經為VS 2010查找了Async 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.