當我嘗試在 GitHub 上獲取最新版本的 repo 時,我收到了 404 回應。使用瀏覽器并訪問例如 url "https://api.github.com/repos/python/cpython/releases/latest/" 為我提供了正確的 JSON,但使用 C# 我得到了 404 錯誤。該代碼昨天運行良好,但今天在沒有更改任何內容的情況下再次嘗試后,出現錯誤。這是代碼:
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "request");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "token token123"); //token123 is replaced by my token of course
var contentsUrl = $"https://api.github.com/repos/python/cpython/releases/latest/";
var contentsJson = httpClient.GetStringAsync(contentsUrl);
Console.WriteLine(contentsJson.Result);
uj5u.com熱心網友回復:
主要問題在這里:
new AuthenticationHeaderValue("Authorization", "token token123");
例如,正如您在檔案中看到的,第一個引數用于 Scheme Bearer。
new AuthenticationHeaderValue("Bearer", ACCESS_TOKEN);
如果您的 Scheme 是token,那么您應該使用以下內容:
new AuthenticationHeaderValue("token", "token123");
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401334.html
標籤:C# github github-api
上一篇:GitHubActionsYAML用于使用相同管道的不同階段的多個部署映像
下一篇:在github頁面上部署React應用程式時出現錯誤:“react-dom.production.min.js:216TypeError:Object(...)isnotafunction”
