简体   繁体   中英

Google Drive authentication in VB.NET to download a file

My goal is to download a file from Google Drive using the Drive API v3 and VB.NET.

I set up my credentials in the Google console: Under "OAuth 2.0 client IDs" I put " http://localhost " in "Authorized redirect URIs" and in "Authorized JavaScript origins" and have my client secret file. I am on ASP.NET 4.0 using NuGet package Google.Apis.Drive.v3.

The error happens on line "credential = GoogleWebAuthorizationBroker.AuthorizeAsync". It pops up a new Chrome tab and says:

  1. That's an error. Error: redirect_uri_mismatch The redirect URI in the request, http://localhost:9895/authorize/ , does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/[MyClientID]?project=[MyProjectNumber]

However each time I get a different port number.

   Public Function AuthenticateOauth() As DriveService
      ' Request authentication from a user using oAuth2
      Dim clientSecretJson = "C:\WebApps\PeruvianGuineaPig\App_Data\client_secret.json"
      Dim applicationName = "DriveApi"

      Try
         ' Permissions
         Dim scopes As String() = New String() {DriveService.Scope.DriveReadonly}
         Dim credential As UserCredential

         Using stream As New FileStream(clientSecretJson, FileMode.Open, FileAccess.Read)
            Dim credPath As String
            credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
            credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly.GetName.Name)

            ' Requesting Authentication
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                                                                     scopes,
                                                                     "user",
                                                                     CancellationToken.None,
                                                                     New FileDataStore(credPath, True)).Result
         End Using

         ' Create Drive API service
         Dim Service = New DriveService(New BaseClientService.Initializer() With
                                    {.HttpClientInitializer = credential, .ApplicationName = applicationName})

         Return Service
      Catch ex As Exception
         Return Nothing
      End Try
   End Function

I didn't realize I needed to open the project as a Web Site (and pick it from my list of Local IIS Sites) and not simply open the Project/Solution file. It now uses the port number I gave it in IIS when I'm debugging.

I have another issue now, but that's for another question...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM