8

I have a C# .net web application that I use to upload videos to Youtube. This worked about 6 months ago but now it has stopped working. The following code just hangs when I launch my website via Visual Studio on my local computer:

     UserCredential credential;
    using (var auth_stream = new FileStream(Server.MapPath("~/YouTube/client_secrets.json"), FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(auth_stream).Secrets,
            // This OAuth 2.0 access scope allows an application to upload files to the
            // authenticated user's YouTube channel, but doesn't allow other types of access.
            new[] { YouTubeService.Scope.YoutubeUpload },
            "user",
            CancellationToken.None
        );
    }

I have also tried the following after looking at other posts on stack overflow:

     UserCredential credentialS;
     using (var auth_stream = new FileStream(Server.MapPath("~/YouTube/client_secrets.json"), FileMode.Open, FileAccess.Read))
     {
         credentialS = await GoogleWebAuthorizationBroker.AuthorizeAsync(
             new ClientSecrets
             {
                 ClientId = "blahblah-myDetailsBlahBLah.apps.googleusercontent.com",
                 ClientSecret = "myClientSecret",
             },
             // This OAuth 2.0 access scope allows an application to upload files to the
             // authenticated user's YouTube channel, but doesn't allow other types of access.
             new[] { YouTubeService.Scope.YoutubeUpload },
             "user",
             CancellationToken.None
         );
     }

I also notived that I was using version 1.7.0-beta of the Google APIs so I upgraded via PackageManager console to version 1.8.2.

I also generated a new Client Secret via the Google Developer Console and used that in my website.

But after trying these steps the call to GoogleWebAuthorizationBroker.AuthorizeAsync still hangs. I notice that my website in my local browser refreshes as if its attempting to connect but the call is never seccessful.

How can I solve this??

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Harry Boy
  • 4,159
  • 17
  • 71
  • 122
  • open check %appData% folder for a folder called that has something like YouTube in it delete it try again. or just change "user" to "user2" and try again – Linda Lawton - DaImTo Aug 23 '15 at 17:06
  • There was an empty folder Roaming\Google.Apis.Auth so I deleted that and I also changed user to user2 but I still have the same problem – Harry Boy Aug 23 '15 at 17:14
  • Could you have a firewall blocking the authentication server? – Linda Lawton - DaImTo Aug 23 '15 at 17:28
  • I have checked and on my PC the Windows firewall is off. I have also a small C# console application where I can upload files to Youtube using the same above code to authenticate (using a different client_secrets.json file) so it has to be a web application specific issue. – Harry Boy Aug 23 '15 at 17:33
  • Double check the redirect uri in Google Developer console. You can test it useing a client id for native application if that works the problem is the redirect uri in the web client settings – Linda Lawton - DaImTo Aug 23 '15 at 17:35
  • OK I checked the redirect uri in my developer console and its http://www.my_real_web_address.com/oauth2callback and this is identical to the redirect_uris value in my client_secrets.json file. I then copied the client ID from the google developer console section of my client program and copied it into the client_secrets.json of my website and I still see the same problem. Is this what you were asking me to do?? – Harry Boy Aug 23 '15 at 17:42
  • 2
    I have the same issue. Code that worked once no longer works. My workaround is to do the authorization in the Google auhorization web page then kill my application and restart it. Upon restart, it reads the token file deposited in the first step and continues without issue. – Mike Meinz Aug 23 '15 at 19:29
  • @MikeMeinz "My workaround is to do the authorization in the Google auhorization web page" - How exactly do I do this??? Thanks – Harry Boy Aug 24 '15 at 09:00
  • The web page that pops up when the call to GoogleWebAuthorizationBroker.AuthorizeAsync is called. – Mike Meinz Aug 24 '15 at 11:31
  • That is how it always used to work with my project. But now all that happens is my webpage seems to refresh (Or attempt to load) and then nothing happens. I never see the google authorization page. ANy other ideas> – Harry Boy Aug 24 '15 at 11:36
  • 1
    I am sorry but I have no further ideas. – Mike Meinz Aug 24 '15 at 22:33
  • would you mind trying and installing the previous versions nugget package Install-Package Google.Apis.YouTube.v3 -Version 1.9.2.1430 issue tracker probably related to this https://github.com/google/google-api-dotnet-client/issues/590 – Linda Lawton - DaImTo Sep 01 '15 at 09:41
  • Possible duplicate of [GoogleWebAuthorizationBroker.AuthorizeAsync Hangs](https://stackoverflow.com/questions/27573272/googlewebauthorizationbroker-authorizeasync-hangs) – Markus Zeller Jul 23 '17 at 10:46

1 Answers1

1

I also ran into this issue using the Google Calendar API.

My application is scraping notices from various online resources and posting them to a shared google calendar.

It works fine in a development environment but didn't when I deployed it to a VM where it is running as a windows scheduled task every 15 minutes using a service account in that VM.

I remember that when first setting it up I had to go through the authorization step in the browser but it wasn't being triggered when run as a scheduled task.

I could not find a way to get it to prompt through the browser to grant access again.

As the service account C:\Users\\Documents.credentials\calendar-dotnet-quickstart folder was being created just fine, I simply copied the Google.Apis.Auth.OAuth2.Responses.TokenResponse-user from the same folder in my dev environment and it now works fine.

That may not be the best or ultimate solution to this but it works.

I suspect if I logged in as that service user for the scheduled task and ran from the command prompt I would get prompted via the browser to grant the authorization.

Hope this helps someone.

bagadonitz
  • 11
  • 3