我的目标是使用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标签,并说:
- 那是一个错误。错误: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中给它的端口号。 我现在有另一个问题,但这是另一个问题......