使用c# HttpClient授权POST到Vimeo API

本文关键字:Vimeo API POST 授权 HttpClient 使用 | 更新日期: 2023-09-27 18:15:59

我正在尝试对Vimeo API进行POST,但我正在获得401 Authorization Required

这是我的请求代码(我只是发送第一个请求,文档说应该返回给我一个上传的票证ID)。

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.vimeo.com");
var byteArray = Encoding.ASCII.GetBytes(accessToken);
client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var form = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>("type","POST")
            });
var response = await client.PostAsync("/me/videos", form);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();

我还尝试添加访问令牌,像这样:

client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Basic", accessToken);

它总是返回401。

添加访问令牌的正确方法是什么?

使用c# HttpClient授权POST到Vimeo API

标题必须是这样的:

client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", accessToken);