Search code examples
c#asp.netgoogle-data-apiyoutube-data-apioauth-2.0

Youtube API v3 OAuth2 handshake error "connection failed" on Windows server


We want to use the Youtube v3 API in Combination with ASP.NET 4.5/C# to upload videos to a specific youtube channel via a Console Application. The compilation is done in a Visual Studio 2015 on a Windows 7 Professional Development machine.

So we created OAuth2 credentials within the Google Developer Console and use the following code for the OAuth2 authorization handshake

        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
                DataStore = new FileDataStore(GoogleWebAuthorizationBroker.Folder)
            };

            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                initializer,

                new[] { YouTubeService.Scope.Youtube },
                user,
                CancellationToken.None
            );
        }

Everything works fine so far. If no authorization happened before, a browser windows opens (access_type=offline&response_type=code) where we can login to our Youtube account, select a channel, grant the necessary access rights, come back to a browser page localhost:portnumber/authorize?code=XYZ where it is written "Received verification code. You may close this windows", and our console application continues its work. This is the working workflow on our development machine.

However, when we copy the application to a Windows Server 2012, an error occurs. The browser window also opens and we can login and grant the access rights, but the final browser page localhost:portnumber/authorize?code=XYZ fails to load with a browser-specific "connection failed" error page.

It seems like no process is being attached to the (random?) port number to read the received code information, retrieve the tokens and to write it to %AppData%/Google.Apis.Auth. The firewall does not block anything.

Our first guess was a name resolution issue. By default, localhost was resolved to ::1 on the Windows server. Unfortunately it still does not work when we force an IPv4 resolution to 127.0.0.1

Thanks for every suggestion.


Solution

  • Ok, I found the solution.

    We had bind the IIS8 to a specific IP address, not including 127.0.0.1.

    After we added the local IP address via netsh http add iplisten, the webserver was able to bind to the localhost socket for the one-time code based authorization again.