我在创建相册之前创建了一个 Imgur 帐户并获取了我的客户端 ID 和 key 。我的目标是在将其集成到我的应用程序之前在测试环境中编写。
以下是我到目前为止使用的代码的最终版本:
string base64String;
string message;
string album = "abcxyz";
using (Image image = Image.FromFile("c:\\path_to\\image.jpg"))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
base64String = Convert.ToBase64String(imageBytes);
}
}
using (var client = new WebClient())
{
string clientID = "supersecret";
client.Headers.Add("Authorization", $"Client-ID {clientID}");
System.Collections.Specialized.NameValueCollection valueCollection = new System.Collections.Specialized.NameValueCollection();
valueCollection["image"] = base64String;
valueCollection["type"] = "base64";
valueCollection["album"] = album;
try
{
byte[] response = client.UploadValues("https://api.imgur.com/3/upload", "POST", valueCollection);
message = Encoding.ASCII.GetString(response);
}
catch (Exception ex)
{
message = ex.Message;
}
}
当我运行代码时,我收到一条消息,表明我显然不拥有我想要拥有的专辑。具体来说:
error=You are not the owner of album '<album hash>', which means you can't
add images to it. For anonymous albums, use the album deletehash.
我曾认为,既然我在使用我的帐户登录时创建了相册,它应该可以工作 - 但现在我错过了一些东西。任何对此的额外关注将不胜感激。
安迪
最佳答案
这是一个简单易行的两步过程。但是 imgur api 文档让我很难理解。
这是在 JS 中: 所以你应该
⠀ 1. make sure you have these headers and this body and a correct + supported type (The type of the file that's being sent; file, base64 or URL) for me base64
var myHeaders = new Headers();
var formdata = new FormData();
myHeaders.append(
"Authorization",
"Bearer 8efbea9b750ad38aeb2e8384a378456363ddddd" //your access-token
);
formdata.append("image", res);
formdata.append("type", "base64");
formdata.append("name", any_string_you_want);
formdata.append("album", "2ddl3LUD");
var requestOptions = {
method: "POST",
headers: myHeaders,
body: formdata,
};
- make sure you don't include any other extra headers or meta-data like, Client-ID : your-client-id.
额外:如果您没有访问 token ,请返回 imgur 文档生成一个,如果您没有相册 ID,请谷歌搜索如何获取它。
关于c# - 使用 Imgur API 3 上传图像报告我不是相册的所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194434/