Google-api-dotnet-client: GoogleWebAuthorizationBroker.AuthorizeAsync Hang when user closes browser

Created on 14 Apr 2015  路  11Comments  路  Source: googleapis/google-api-dotnet-client

What steps will reproduce the problem?
1. Use GoogleWebAuthorizationBroker.AuthorizeAsync to gain user credentials
2. User nether accepts nor rejects permission, but closes browser window/tab
3. Works on Calendar.VB.ConsoleApp, haven't tried others.

What is the expected output? What do you see instead?
An exception to be thrown which could be caught.
Program stays in loop w/o exit, even with await clause.

What version of the product are you using?
What is your operating system? Win7 and Win8
What is your IDE? VS2013
What is the .NET framework version? 4.0 and 4.5

Please provide any additional information below.
Others from SE with same issue:

http://stackoverflow.com/questions/24659654/google-drive-catch-events-during-aut
hentication
http://stackoverflow.com/questions/22995218/google-drive-oauth-connect-probem  
[Note, setting Await still wasn't able to wrap error]
http://stackoverflow.com/questions/23475498/oauth-lockup-google


Original issue reported on code.google.com by [email protected] on 19 Nov 2014 at 2:43

bug

Most helpful comment

We can throw timeout exception using thread when the thread meets the timeout value, below is the code and it serves my purpose. Here i have set the timeout value to 2 mins (120,000 milliseconds).

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(SystemDataUtils.ConvertToJson())))
{
UserCredential cred = null;

            var thread = new Thread(() =>
                                        cred = (GoogleWebAuthorizationBroker.AuthorizeAsync(
                                            GoogleClientSecrets.Load(stream).Secrets,
                                            Scopes,
                                            userName,
                                            CancellationToken.None,
                                            new FileDataStore(FileDataStore)).Result)
            )
            { IsBackground = false };

            thread.Start();
            if (!thread.Join(120000))
            {
                throw new Exception("Timeout exception..!!!");
            }
            else
            {
                return cred;
            }
        }

All 11 comments

Also happens in my C# VSTO, which can cause Excel to hang and force termination 
at the process.

Original comment by [email protected] on 19 Nov 2014 at 2:44

Original comment by [email protected] on 23 Nov 2014 at 3:26

  • Changed state: Accepted
  • Added labels: Milestone-Release1.9.1

Original comment by [email protected] on 23 Dec 2014 at 1:49

  • Added labels: Milestone-Release1.9.2
  • Removed labels: Milestone-Release1.9.1
I got the same issue.
Tried to fix this like that : 

using (var cts = new CancellationTokenSource())
{
    cts.CancelAfter(TimeSpan.FromMinutes(1));

    return GoogleWebAuthorizationBroker.AuthorizeAsync(
                            clientSecrets,
                            new[] { GOOGLE_SITES_SCOPE },
                            "user",
                            cts.Token,
                            _fileDataStore).Result;
}

but cancellation does not always occur.

Original comment by [email protected] on 1 Apr 2015 at 8:50

I tried the same thing but with a longer TimeSpan.  I haven't been able to 
figure out why the timeout won't work in some cases.  I have a higher successes 
rate in my console apps than VSTO, so I think it might have something to do 
with the starting environment.

Original comment by [email protected] on 1 Apr 2015 at 1:39

Closing as stale. Please open a new issue if follow-up is required.

As far as I know this is still an issue there is no time out on the authentication.

It is not possible, in general, to detect when the user has closed the browser window/tab.
For example, if the user is already running their default browser, then GoogleWebAuthorizationBroker opens a new tab, this is not running in the process started in LocalServerCodeReceiver. That new process terminates immediately.

However, currently the CancellationToken passed to GoogleWebAuthorizationBroker.AuthorizeAsync is not honoured whilst waiting for the auth response. #968 filed to fix this.

As long as we have a way of timing it out it should be fine. I remember looking at this years ago never found a solution.

We can throw timeout exception using thread when the thread meets the timeout value, below is the code and it serves my purpose. Here i have set the timeout value to 2 mins (120,000 milliseconds).

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(SystemDataUtils.ConvertToJson())))
{
UserCredential cred = null;

            var thread = new Thread(() =>
                                        cred = (GoogleWebAuthorizationBroker.AuthorizeAsync(
                                            GoogleClientSecrets.Load(stream).Secrets,
                                            Scopes,
                                            userName,
                                            CancellationToken.None,
                                            new FileDataStore(FileDataStore)).Result)
            )
            { IsBackground = false };

            thread.Start();
            if (!thread.Join(120000))
            {
                throw new Exception("Timeout exception..!!!");
            }
            else
            {
                return cred;
            }
        }
Was this page helpful?
0 / 5 - 0 ratings