调用基础 REST API

Calling Base REST API

我正在尝试调用需要接受和授权 headers 的基地 API。我在控制台应用程序中有以下代码,但总是收到 400 错误。我已经拦截了 fiddler 中的调用并且 headers 不存在。如果我在 fiddler 中手动创建它,则该调用有效。我是否遗漏了请求中的某些内容?

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://api.getbase.com/");

    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

    HttpResponseMessage response = await client.GetAsync("v2/users/self");

    if (response.IsSuccessStatusCode)
    {

    }
}

这是文档https://developers.getbase.com/docs/rest/articles/first_call

您应该将 User-Agent header 添加到请求中,因为错误消息会告诉您这样做。

error":{"code":"invalid_header","message":"invalid request header","details":"The header 'User-Agent' is malformed, missing, or has an invalid value."

以下示例在使用 curl php 时有效,请注意带有 'User-Agent' 的行 不要忘记 $ACESS_TOKEN。

$ch = curl_init('https://api.getbase.com/v2/leads');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'User-agent: '.$_SERVER['HTTP_USER_AGENT'], // USER-AGENT 
    'Content-Type: application/json; charset=utf-8',
    'Authorization: Bearer ' .$ACCESS_TOKEN
    )
);