0

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
p0tato
  • 71
  • 2
  • 10
  • Not the source of your problem, but you are [blocking on async code](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – GSerg Mar 28 '19 at 19:14
  • 2
    As for the source of the problem, are you debugging in VS [on a random port](https://stackoverflow.com/a/32968004/11683)? – GSerg Mar 28 '19 at 19:21
  • @GSerg yes I am debugging in VS on a random port, but I am having trouble trying to figure out how to follow those steps in the link you provided. I'm using VS 2013 and when I right-click, Properties, the solution in the solution explorer, the menu doesn't have a web tab. – p0tato Mar 28 '19 at 19:33

1 Answers1

0

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...

p0tato
  • 71
  • 2
  • 10