gpt4 book ai didi

c# - 使用 RESTSHARP 时 API 返回错误

转载 作者:太空狗 更新时间:2023-10-29 20:46:31 25 4
gpt4 key购买 nike

当使用 RestSharp 调用 API 时出现此错误:

The underlying connection was closed: An unexpected error occurred on a send.

我已验证我的客户端 ID、密码、用户名和密码正确无误。我能够在 PowerShell 中毫无问题地执行此操作。

public string GetTokenForBrightIdea()
{
RestClient restclient = new RestClient(_uri);
RestRequest request = new RestRequest() { Method = Method.POST };

request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", _clientId);
request.AddParameter("client_secret", _clientSecret);
request.AddParameter("username", _clientUsername);
request.AddParameter("password", _clientPassword);

var tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
return JsonConvert.DeserializeObject<Dictionary<string, object>>(
responseJson)["access_token"].ToString();
}

使用 RestSharp 完成这项工作时,我错过了什么?

最佳答案

事实证明,因为这个调用是 HTTPS,所以我需要添加以下代码行

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

关于c# - 使用 RESTSHARP 时 API 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549161/

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