Getting image info anonymously through Imgur API in C#
我试图通过C#通过Imgur版本3 API匿名获取图像数据(如图像大小)。 他们的文件说明
The API requires each client to use OAuth 2 authentication. This means
you'll have to register your application, and generate an access_code
if you'd like to log in as a user.For public read-only and anonymous resources, such as getting image
info, looking up user comments, etc. all you need to do is send an
authorization header with your client_id in your requests. This also
works if you'd like to upload images anonymously (without the image
being tied to an account), or if you'd like to create an anonymous
album. This lets us know which application is accessing the API.Authorization: Client-ID YOUR_CLIENT_ID
因此,我将Client-ID作为标头添加到HttpWebRequest中。 这是我的下面的代码。
1 2 3 4 5 6 7 8 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image/id/8ABRUYt"); webRequest.Headers.Add("Authorization","Client-ID XXXXX"); Stream response = webRequest.GetResponse().GetResponseStream(); StreamReader reader = new StreamReader(response); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer); reader.Close(); response.Close(); |
我收到404错误,但该图片显然存在-> http://imgur.com/8ABRUYt(银河栏的图片)。 我做错什么了吗?
您的第一行应为
1 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image/8ABRUYt"); |
正确的网址是" https://api.imgur.com/3/image/{id}"-{id}是您的图片ID。
您的帖子帮助我从简单地从imgur中查看图片起步! 我会投票给你,但是那需要15个声誉:(我会在可能的时候一定会答谢你的:)