【问题标题】:Calling Base REST API调用基本 REST API
【发布时间】:2015-06-28 21:57:03
【问题描述】:

我正在尝试调用需要 Accept 和 Authorization 标头的 Base API。我在控制台应用程序中有以下代码,但总是收到 400 错误。我已经在提琴手中截获了电话,并且标头不存在。如果我在 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

【问题讨论】:

标签: c# .net rest http-headers dotnet-httpclient


【解决方案1】:

以下示例在使用 curl php 时有效,请注意带有 'User-Agent' 的行 不要忘记 $ACES_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
    )
);

【讨论】:

    【解决方案2】:

    您应该在请求中添加User-Agent 标头,因为错误消息告诉您这样做。

    error":{"code":"invalid_header","message":"无效请求 header","details":"标头 'User-Agent' 格式错误、丢失或 具有无效值。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 2015-05-20
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 2018-09-06
      • 2019-06-20
      • 2016-05-04
      相关资源
      最近更新 更多