Getting image info anonymously through Imgur API in C#

I'm trying to anonymously get image data (like image sizes) through the Imgur Version 3 API through C#. Their documentation states

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

So I added the Client-ID as a header to my HttpWebRequest. Here's my code below.

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();

I get a 404 error, but that image clearly exists -> http://imgur.com/8ABRUYt (Picture of a milky way bar). Am I doing anything wrong?


Your first line should read

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image/8ABRUYt");

The correct url is "https://api.imgur.com/3/image/{id}" - {id} being your picture ID.

Your post helped me greatly getting started on simply viewing a picture from imgur! I would vote you up, but that requires 15 reputation :( I'll be sure to rep you, when I can :)

链接地址: http://www.djcxy.com/p/14294.html

上一篇: STM对于某些TVar具有部分原子性

下一篇: 通过Imgur API在C#中匿名获取图像信息