3

Googleドライブにファイルをアップロードするためのデスクトップアプリケーションが.netにあります。プログラムは正しく実行されますが、PCにプロキシが構成されている場合、プログラムはメソッドUpload(){ FilesResource.InsertMediaUpload.Upload()}の実行に失敗します。メッセージエラーは次のとおりです。

DotNetOpenAuth.Messaging.ProtocolException:{"'https://accounts.google.com/o/oauth2/token'へのウェブリクエストに失敗しました。"}

リモートサーバーエラー:(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