gpt4 book ai didi

c# - 如何使用 HttpClient 进行 OAuth2.0 请求 västrafik api

转载 作者:行者123 更新时间:2023-11-30 11:18:33 26 4
gpt4 key购买 nike

如何在C#中使用HttpClient? 。我曾尝试实现这一点,但没有成功。我被困住了,感谢一些帮助:)

波纹管是一些正在运行的快速代码,然后是显示我尝试做的事情的 C# 代码。提前致谢。

生成 token :

 POST https://api.vasttrafik.se/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Authorization: Basic UmJseEkyeTFsWVNFTTZ0Z2J6anBTa2E0R1o6Wk1nSkR0Y0paRGV4OTJldUxpQUdYOFExUnU=
grant_type=client_credentials&scope=<device_id>

快速代码:

let data = ("\(Constants.key):\(Constants.secret)").data(using: String.Encoding.utf8)
let base64 = data!.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))

let parameters = [
"grant_type": "client_credentials",
"scope": "\(UIDevice.current.identifierForVendor!.uuidString)"
]

let headers = [
"Authorization": "Basic \(base64)",
"Content-Type": "application/x-www-form-urlencoded"
]

if let expires = UserDefaults.standard.object(forKey: "expires") as? Date, let token = UserDefaults.standard.object(forKey: "token") as? String {
if expires > Date() {
completionHandler(token)
return
}
}

Alamofire.request(Constants.tokenURL, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
if let json = response.result.value as? [String:Any] {
if let token = json["access_token"] as? String, let expires = json["expires_in"] as? Int {
let date = Date()
let expiresTime = Calendar.current.date(byAdding: .second, value: expires, to: date)
UserDefaults.standard.set(expiresTime, forKey: "expires")
UserDefaults.standard.set(token, forKey: "token")
completionHandler(token)
}
}
}

我尝试过这样做,但没有成功。 C# 代码:

            var client = new HttpClient();
client.Timeout = new TimeSpan(0, 0, 10);
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_nyckel + ":" + _hemlighet);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(plainTextBytes));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));


/*
* Connect to the server
*/


var strin = Injected.Instance.Platform.GetId();
var base54 = System.Convert.ToBase64String(plainTextBytes);

var stringContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials&scope=" + Injected.Instance.Platform.GetId())
});

HttpResponseMessage response = await client.PostAsync(page, stringContent);
response.EnsureSuccessStatusCode();

最佳答案

我使用以下代码解决了这个问题。

using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.vasttrafik.se");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("scope", Injected.Instance.Platform.GetId())
});

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(plainTextBytes));

content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var result = await client.PostAsync("token",content);
string resultContent = await result.Content.ReadAsStringAsync();

}

关于c# - 如何使用 HttpClient 进行 OAuth2.0 请求 västrafik api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com