Getting image info anonymously through Imgur API in C#
Asked Answered
A

1

10

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 -> https://i.sstatic.net/1NJK2.jpg (Picture of a milky way bar). Am I doing anything wrong?

Aubergine answered 17/6, 2013 at 4:19 Comment(0)
I
5

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

Indigence answered 28/6, 2013 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.