Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleWebAuthorizationBroker.AuthorizeAsync() hangs if browser closed

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?

like image 404
James Screech Avatar asked Oct 25 '25 05:10

James Screech


1 Answers

This is a known issue in the Google .net Client library.

  1. GoogleWebAuthorizationBroker.AuthorizeAsync Hang when user closes browser
  2. No time out on GoogleWebAuthorizationBroker.AuthorizeAsync?

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

like image 78
DaImTo Avatar answered Oct 26 '25 20:10

DaImTo