Google API V3 OAUTH2 Hang - TokenResponse-user Cre

2019-05-10 04:10发布

问题:

Google API V3 OAUTH2 Problem Report

The GoogleWebAuthorizationBroker.AuthorizeAsync code below works fine on my Windows 8.1. When I install on my Windows 7 PC and run the EXE, the AuthorizeAsync is executed, the browser opens and I click through the Select an account page and the This app would like to: page and then see a page for a short time that says Received verification code. Closing... There is no TokenResponse-user file saved in the ..\AppData\Roaming\Google.Apis.Auth folder and my EXE hangs (waits forever). I have tried with both Chrome and Internet Explorer browsers.

Works fine on Windows 8.1 PC but hangs on Windows 7 PC. I tried two different Windows 7 PCs. The same Client ID/Secret and same Google Username/Password were used on all of the PCs.

Is there something different I should be doing in my code or is this a problem with the Google API V3 client libraries?

Private Async Function GetGredentials() As Task
        OAUth2Credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync( _
                New ClientSecrets With {.ClientId = Google_ClientID, .ClientSecret = Google_ClientSecret}, _
                {YouTubeService.Scope.Youtube}, "user", CancellationToken.None)
End Function

Versions on my PCs:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Google.Apis" version="1.8.2" targetFramework="net40" />
  <package id="Google.Apis.Auth" version="1.8.2" targetFramework="net40" />
  <package id="Google.Apis.Core" version="1.8.2" targetFramework="net40" />
  <package id="Google.Apis.YouTube.v3" version="1.8.1.1080" targetFramework="net40" />
  <package id="log4net" version="2.0.3" targetFramework="net40" />
  <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net40" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
  <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
  <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="6.0.3" targetFramework="net40" />
  <package id="Zlib.Portable" version="1.10.0" targetFramework="net40" />
</packages>

The Chrome History shows this but there is no file in the Google.Apis.Auth folder:

9:50 AM OAuth 2.0 Authentication Token Received localhost
9:50 AM Request for Permission accounts.google.com

回答1:

I have found a solution. Here is how I arrived at the solution.

I decided to try copying the Google.Apis.Auth.OAuth2.Responses.TokenResponse-user from my Windows 8.1 PC to my Windows 7 PC to see if a refresh of the token file would work because a first time creation of the token file did not work. A refresh did work. The GoogleWebAuthorizationBroker.AuthorizeAsync did a refresh of the token file and exited properly back to my EXE program.

But, another problem surfaced.

In the next step of my EXE program, while executing a ChannelsResource.ListRequest, I got this error message box:

Could not load type 'System.Net.HttpStatusCode' from assembly 'System.Net, Version 4.0.9.9, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

A search revealed that someone else had seen the same error and was advised to install Microsoft .NET Framework 4.0 update KB2468871. I did so and now my ChannelsResource.ListRequest executed properly.

I deleted the Google.Apis.Auth.OAuth2.Responses.TokenResponse-user token file and re-ran my EXE program executing the GoogleWebAuthorizationBroker.AuthorizeAsync to create a new token file. This time it worked properly.

My conclusion is that the same Could not load type 'System.Net.HttpStatusCode' exception occurred during the execution of GoogleWebAuthorizationBroker.AuthorizeAsync but it was not trapped and reported.

Google API V3 Documentation

The following prerequisite requirement should be added to the Google API V3 documentation:

Install Microsoft .NET Framework 4.0 update KB2468871 (http://www.microsoft.com/en-us/download/details.aspx?id=3556)

N.B. Later, after experiencing yet another problem, I discovered that the Windows 7 PC did not have all of the latest updates from Windows Update. Using Windows Update to get all of the updates solved the problem documented in System.InvalidOperationException in MSCORLIB when Google API V3 .UploadAsync is called

Further Information

I had to manually add the following files that were installed in the project's folder from NuGet into the InstallShield LE Setup Project's Files tab so that they would be copied to the target system. For some reason, the InstallShield LE Setup project did not automatically add these files. It added all of the others.

System.Net.Http.dll
System.Net.Http.WebRequest.dll
System.Runtime.dll
System.Threading.Tasks.dll