簡體   English   中英

在VB.NET中使用Google Drive認證來下載文件

[英]Google Drive authentication in VB.NET to download a file

我的目標是使用Drive API v3和VB.NET從Google雲端硬盤下載文件。

我在Google控制台中設置了我的憑據:在“OAuth 2.0客戶端ID”下,我將“ http:// localhost ”放在“授權重定向URI”和“授權的JavaScript來源”中,並擁有我的客戶端密碼文件。 我使用NuGet包Google.Apis.Drive.v3在ASP.NET 4.0上。

錯誤發生在“credential = GoogleWebAuthorizationBroker.AuthorizeAsync”行。 它會彈出一個新的Chrome標簽,並說:

  1. 那是一個錯誤。 錯誤:redirect_uri_mismatch請求中的重定向URI( http:// localhost:9895 / authorize /)與授權給OAuth客戶端的URI不匹配。 要更新授權的重定向URI,請訪問: https//console.developers.google.com/apis/credentials/oauthclient/ [MyClientID]? project = [MyProjectNumber]

但是每次我得到一個不同的端口號。

   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

我沒有意識到我需要將項目作為網站打開(並從我的本地IIS站點列表中選擇)而不是簡單地打開項目/解決方案文件。 它現在使用我在調試時在IIS中給它的端口號。

我現在有另一個問題,但這是另一個問題......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM