簡體   English   中英

在C#/ Mono中,無法與Google雲存儲通信

[英]In C#/Mono, not able to communicate with google cloud storage

我現在正在開發一個可通過Mono在Linux實例上運行的C#應用​​程序。 處理完圖像后,需要將圖像存儲在Google Cloud Storage中。

我具有身份驗證憑據設置,並且.NET客戶端庫能夠設置StorageService對象(即,找到JSON文件並驗證所有內容)。 當我嘗試上傳時,沒有任何反應。 因此,在我的SetupConnection()函數中,我嘗試查詢項目中的單個存儲桶。 我收到此錯誤:

Unhandled Exception:
System.Net.WebException: Error writing request: The authentication or decryption has failed.
  at System.Net.WebConnectionStream.WriteHeaders () [0x00000] in <filename unknown>:0 
  at System.Net.WebConnectionStream.SetHeaders (System.Byte[] buffer) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Net.WebConnectionStream:SetHeaders (byte[])
  at System.Net.HttpWebRequest.SendRequestHeaders (Boolean propagate_error) [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Http.ConfigurableMessageHandler+<SendAsync>d__43.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.Requests.TokenRequestExtenstions+<ExecuteAsync>d__0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[Google.Apis.Auth.OAuth2.Responses.TokenResponse].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceAccountCredential+<RequestAccessTokenAsync>d__19.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Boolean].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceCredential+<GetAccessTokenForRequestAsync>d__23.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.String].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceAccountCredential+<GetAccessTokenForRequestAsync>d__20.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.String].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceCredential+<InterceptAsync>d__21.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Http.ConfigurableMessageHandler+<SendAsync>d__43.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Requests.ClientServiceRequest`1+<ExecuteUnparsedAsync>d__26[Google.Apis.Storage.v1.Data.Buckets].MoveNext () [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.WebException: Error writing request: The authentication or decryption has failed.
  at System.Net.WebConnectionStream.WriteHeaders () [0x00000] in <filename unknown>:0 
  at System.Net.WebConnectionStream.SetHeaders (System.Byte[] buffer) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Net.WebConnectionStream:SetHeaders (byte[])
  at System.Net.HttpWebRequest.SendRequestHeaders (Boolean propagate_error) [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Http.ConfigurableMessageHandler+<SendAsync>d__43.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.Requests.TokenRequestExtenstions+<ExecuteAsync>d__0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[Google.Apis.Auth.OAuth2.Responses.TokenResponse].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceAccountCredential+<RequestAccessTokenAsync>d__19.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Boolean].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceCredential+<GetAccessTokenForRequestAsync>d__23.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.String].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceAccountCredential+<GetAccessTokenForRequestAsync>d__20.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.String].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Auth.OAuth2.ServiceCredential+<InterceptAsync>d__21.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Http.ConfigurableMessageHandler+<SendAsync>d__43.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00000] in <filename unknown>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in <filename unknown>:0 
  at Google.Apis.Requests.ClientServiceRequest`1+<ExecuteUnparsedAsync>d__26[Google.Apis.Storage.v1.Data.Buckets].MoveNext () [0x00000] in <filename unknown>:0 

這是我的設置代碼:

public static void SetupGCSConnection()
{
    var credentials = Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync().Result;

    if (credentials.IsCreateScopedRequired)
        credentials = credentials.CreateScoped(new[] { StorageService.Scope.DevstorageFullControl });

    var serviceInitializer = new BaseClientService.Initializer()
    {
        ApplicationName = "MyApp",
        HttpClientInitializer = credentials
    };

    storage = new StorageService(serviceInitializer);

    // Error is thrown here
    var buckets = storage.Buckets.List(Config.GoogleProjectID).Execute();
    foreach (var b in buckets.Items) {
        Console.WriteLine(b.ToString());
    }
}

我以前可以讓C#與GCS進行對話,但是那是我在Windows環境中使用Microsoft的.NET vm進行工作的時候。 我之前做了一些研究,Mono處理CA的方式與Microsoft VM處理的方式可能存在一些問題。

您的問題通常來自未更新的根證書。

請按照此處的說明更新您的根證書。

如果這樣做沒有幫助,則可以將委托設置為

ServicePointManager.ServerCertificateValidationCallback

並接受所有證書。 如果可行,那么您知道問題出在證書驗證上,您可以嘗試其他解決方案。 (我絕不建議使用回調函數來接受生產環境中的所有證書,僅使用它來調試問題)。

我解決了這個問題。

我使用的是Ubuntu隨附的默認Mono版本,但該版本已過時。 我在這里改用了官方的Mono庫: http : //www.mono-project.com/docs/getting-started/install/linux/

然后也安裝了ca-certificates-mono軟件包。 (此處有更多詳細信息: http : //www.mono-project.com/docs/faq/security/

感謝Lex Li的幫助。

暫無
暫無

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

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