Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image info anonymously through Imgur API in C#

Tags:

json

c#

api

imgur

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?

like image 413
Narabhut Avatar asked Jun 17 '13 04:06

Narabhut


1 Answers

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 :)

like image 78
Kiesp Avatar answered Oct 02 '22 19:10

Kiesp