I'm writing a C# desktop app that can copy its output to a users Google Drive. However when I try an authorise access to Google with GoogleWebAuthorizationBroker.AuthorizeAsync() if the user closes the authentication page in the browser the method never returns!
Even if I use a 30 sec cancellation token it never returns if the browser is closed.
public bool Authorise()
{
_authorised = false;
UserCredential credential = null;
try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
CancellationTokenSource cts = new CancellationTokenSource(new TimeSpan(0,0,30));
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = this.ClientId, ClientSecret = this.ClientSecret },
Scopes,
UserName,
cts.Token,//CancellationToken.None,
new FileDataStore(AppName)).Result;
}
catch (Exception)
{
return _authorised;
}
if ((credential != null) && (credential.Token.AccessToken != null))
{
Service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = AppName, });
_authorised = true;
}
return _authorised;
}
How can I trap the user closing the browser window and stop my application hanging?
This is a known issue in the Google .net Client library.
There is currently no workaround that I know of. I recommend adding your name to the issues on the forum to encourage the team to address the issue.
Update: Its now on the list GoogleWebAuthorizationBroker does not honour CancellationToken when waiting for browser response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With