3

我在 .net 中有一个桌面应用程序,可以将文件上传到 Google Drive。程序运行正常,但如果 PC 配置了代理,则程序无法执行方法Upload(){ FilesResource.InsertMediaUpload.Upload()}。消息错误是:

DotNetOpenAuth.Messaging.ProtocolException: {"对 'https://accounts.google.com/o/oauth2/token' 的 Web 请求失败。"}

远程服务器错误:(407) 需要代理身份验证。

我认为问题在于身份验证请求;因此有必要配置 OAuth2Authenticator 对象以使用代理。

这是代码:

Public Function getAuthenticator() As OAuth2Authenticator(Of AssertionFlowClient)
      Dim SERVICE_ACCOUNT_EMAIL As String = <email>
      Dim SERVICE_ACCOUNT_PKCS12_FILE_PATH As String = <pathFile>
      Dim certificate As X509Certificate2 = New X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, <secret>, X509KeyStorageFlags.Exportable)
      Dim provider = New AssertionFlowClient(GoogleAuthenticationServer.Description, certificate) With {.ServiceAccountId = SERVICE_ACCOUNT_EMAIL, .Scope = DriveService.Scopes.Drive.GetStringValue}
      Dim auth = New OAuth2Authenticator(Of AssertionFlowClient)(provider, AddressOf AssertionFlowClient.GetState)

      Return auth
End Function

Public Function getService() As DriveService
    Return New DriveService(getAuthenticator())
End Function

Public Function upload(pathFile As String, fileName As String) As File
    Dim service = getService(False)
    Dim fichero As File = New File()
    Dim byteArray As Byte() = System.IO.File.ReadAllBytes(pathFile)
    Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)

    fichero.Title = fileName
    Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(fichero, stream, "*/*")

    request.Upload()   'ERROR with proxy

    Dim fileUpload As File = request.ResponseBody

    Return fileUpload
End Function

谢谢。

4

0 回答 0