PHP转C# RestSharp API调用遇Authorization头错误,求排查优化
我正尝试将一段PHP的API POST调用转换为C# RestSharp实现,已完全复刻PHP中的逻辑,但仍收到错误响应:"Authorization header is incorrect"。我不熟悉PHP,能否帮我检查RestSharp代码是否与PHP代码完全一致,以及可优化的点?
PHP示例代码
<? $clientId = "your_client_id"; $secret = "your_secret"; $url = "https://www.badboy.com/api/1.1/request/create"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); $headers = array( "Accept: application/json", "Content-Type: application/json", "Authorization: Basic " . base64_encode($clientId . ":" . $secret) ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $data = [ "reference"=>"some-reference-value", "environment"=>"sandbox", "name"=>"John Smith", "email"=>"myemail@email.com", "scope"=>"documents(passport)", "address" => [ "address1"=>"Unit 8-10", "town"=>"London", "postcode"=>"SW6 2UZ", "country"=>"GBR" ] ]; curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); ?>
C# RestSharp代码(原实现)
string endpoint = @"https://www.badboy.com/"; string clientId = @"your_client_id"; string secretCode = @"your_secret"; var client = new RestClient(endpoint); var request = new RestRequest("api/1.8/request/create", Method.Post); request.RequestFormat = DataFormat.Json; string combine = clientId + ":" + secretCode; var bt = Encoding.UTF8.GetBytes(combine); string encodedSecretCode = Convert.ToBase64String(bt); //request.AddHeader("Accept", "application/json"); //request.AddHeader("Content-Type", "application/json"); //request.AddHeader("Authorization", "Basic " + encodedSecretCode); Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("Accept", "application/json"); headers.Add("Content-Type", "application/json"); headers.Add("Authorization", "Basic " + encodedSecretCode); request.AddHeaders(headers); var data = new DataBody(); data.reference = "some-dd-value"; data.environment = "ss"; data.name = "John Smith"; data.email = "me@email.com"; data.scope = "documents(foo)"; data.address.address1 = "Unit 8-10"; data.address.town = "London"; data.address.postcode = "SW6 2UZ"; data.address.country = "GBR"; string jsonData = JsonSerializer.Serialize(data); request.AddBody(jsonData); var response = client.Execute(request);
C#模型类
public class Address { public string address1 { get; set; } public string address2 { get; set; } public string address3 { get; set; } public string town { get; set; } public string postcode { get; set; } public string country { get; set; } } public class DataBody { public string reference { get; set; } public string client_key { get; set; } public string note { get; set; } public string scope { get; set; } public string callback_uri { get; set; } public string name { get; set; } public string redirect_uri { get; set; } public string mandatory { get; set; } public string user { get; set; } public string email { get; set; } public string send_choice { get; set; } public string tabs_order { get; set; } public string environment { get; set; } public bool use_defaults { get; set; } public Address address { get; set; } = new Address(); }
问题排查与优化点
1. API版本不一致
PHP调用的是api/1.1/request/create,而C#用的是api/1.8/request/create,版本差异可能导致接口鉴权逻辑变化,必须先对齐API版本号。
2. Basic Auth实现错误
PHP同时设置了手动拼接的Authorization头和CURLAUTH_BASIC,但RestSharp有原生的Basic Auth实现,手动拼接容易出现编码或格式问题。建议替换为RestSharp自带方法:
// 替换手动拼接Authorization头的代码 request.AddBasicAuthentication(clientId, secretCode);
此方法会自动处理编码和头的生成,避免手动拼接的错误。
3. 请求Body处理冗余
你手动序列化DataBody再调用AddBody,但RestSharp在设置RequestFormat = DataFormat.Json后,可以直接传入对象,它会自动用内置的序列化器处理,避免手动序列化导致的格式不兼容:
// 替换手动序列化的代码 request.AddBody(data);
4. 请求参数不匹配
对比PHP和C#的参数值:
- PHP中
environment为sandbox,C#中是ss - PHP中
scope为documents(passport),C#中是documents(foo) - PHP中
reference为some-reference-value,C#中是some-dd-value
这些参数差异可能不仅影响鉴权,还会导致接口逻辑报错,必须将参数值对齐。
5. 模型类多余字段处理
DataBody包含很多PHP请求中没有的字段(如client_key、note等),默认序列化会将这些null值或默认值(如use_defaults的false)传入接口,可能不符合要求。可以用[JsonIgnore]标记不需要的字段:
public class DataBody { public string reference { get; set; } [JsonIgnore] public string client_key { get; set; } [JsonIgnore] public string note { get; set; } public string scope { get; set; } [JsonIgnore] public string callback_uri { get; set; } public string name { get; set; } [JsonIgnore] public string redirect_uri { get; set; } [JsonIgnore] public string mandatory { get; set; } [JsonIgnore] public string user { get; set; } public string email { get; set; } [JsonIgnore] public string send_choice { get; set; } [JsonIgnore] public string tabs_order { get; set; } public string environment { get; set; } [JsonIgnore] public bool use_defaults { get; set; } public Address address { get; set; } = new Address(); }
优化后的完整C#代码
string endpoint = @"https://www.badboy.com/"; string clientId = @"your_client_id"; string secretCode = @"your_secret"; var client = new RestClient(endpoint); // 对齐API版本号 var request = new RestRequest("api/1.1/request/create", Method.Post); request.RequestFormat = DataFormat.Json; // 使用RestSharp原生Basic Auth request.AddBasicAuthentication(clientId, secretCode); var data = new DataBody(); // 对齐参数值 data.reference = "some-reference-value"; data.environment = "sandbox"; data.name = "John Smith"; data.email = "myemail@email.com"; data.scope = "documents(passport)"; data.address.address1 = "Unit 8-10"; data.address.town = "London"; data.address.postcode = "SW6 2UZ"; data.address.country = "GBR"; // 直接传入对象,RestSharp自动序列化 request.AddBody(data); var response = client.Execute(request);
内容的提问来源于stack exchange,提问作者John Decor
相关产品推荐
相关产品推荐

