0

我正在尝试将 .mp4 文件上传到 Giphy.com 的 API。它说以“二进制”形式发送文件,我想我对它们的确切含义感到困惑。如果您在“上传端点”滚动到底部,这里是文档。https://developers.giphy.com/docs/

这就是我现在所拥有的。

我已经尝试了多个版本(使用StringContent, MultipartFormDataContent, ByteArrayContent, HttpMessages... 等)并且总是得到一个“400 - Bad Request - No Source Url”(如果你上传你自己的,文档说不需要)让我相信内容没有被识别。

    public async Task<HttpResponseMessage> UploadVideoAsync(StorageFile file)
    {
        using (var stream = await file.OpenStreamForReadAsync())
        {
            byte[] bytes = new byte[stream.Length];
            await stream.ReadAsync(bytes, 0, (int)stream.Length);

            Dictionary<string, string> dic = new Dictionary<string, string>
            {
                { "file", Encoding.ASCII.GetString(bytes) },
                { "api_key", api_key }
            };

            MultipartFormDataContent multipartContent = new MultipartFormDataContent();
            multipartContent.Add(new ByteArrayContent(bytes));
            var response = await httpClient.PostAsync($"v1/gifs?api_key={api_key}", multipartContent);
            var stringResponse = await response.Content.ReadAsStringAsync();
            return response;

        }
    }
4

2 回答 2

0

您的代码似乎与 {api_key} 不正确匹配。您不会在任何地方使用“dic”变量。你可以试试v1/gifs?api_key=YOUR_API_KEY&file=。其中 YOUR_API_KEY 应替换为从 giphy 获得的 API 密钥。

于 2018-08-15T08:34:03.623 回答
0

总是得到一个“400 - 错误请求 - 无源网址”(如果你上传你自己的,文档说不需要),这让我相信内容没有被识别。

您需要为ByteArrayContent. 该文档显示Request Parameters包含“如果未提供 source_image_url,则需要文件:字符串(二进制)”。

代码应如下所示:

MultipartFormDataContent multipartContent = new MultipartFormDataContent();
multipartContent.Add(new ByteArrayContent(bytes),"file");
于 2018-08-16T07:48:05.000 回答