4

I'm trying to add this functionality to a C# Windows app I have in development, for uploading images to Imgur.

Unfortunately it has to be Imgur, as that site is the requirement.

The problem is that whatever C# example code I can find is old and doesn't seem to work with their version 3 API.

So I was wondering if anyone with expertise in the area can help me out.

I would prefer to upload using OAuth, and not the Anonymous option, but the latter can be used as an example if needed.

EDIT:

One part I especially don't get is how can I make the authorization step happen while remaining within the desktop application. The authorization step requires the opening of a webpage, where the user is asked if they will allow the application to use their data or not.

How can I do this for a Desktop-based app?

Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55
Ahmad
  • 12,886
  • 30
  • 93
  • 146

1 Answers1

7

Before you start you´ll need to register your Application to recieve a clientID and the client secret. Guess you´re aware of that already. Detailed information can be found on the official Imgur API Documentation.

Regarding authentication you´re right, the user has to visit a webpage and grab and authorize your appication there. You can either embed some Webbrowser control in your Application or simply instruct the user to browse to a webpage.

Here´s some untested Code that should work with little modifications.

class Program
    {
        const string ClientId = "abcdef123";
        const string ClientSecret = "Secret";

        static void Main(string[] args)
        {
            string Pin = GetPin(ClientId, ClientSecret);
            string Tokens = GetToken(ClientId, ClientSecret, Pin);

            // Updoad Images or whatever :)
        }

        public static string GetPin(string clientId, string clientSecret)
        {
            string OAuthUrlTemplate = "https://api.imgur.com/oauth2/authorize?client_id={0}&response_type={1}&state={2}";
            string RequestUrl = String.Format(OAuthUrlTemplate, clientId, "pin", "whatever");
            string Pin = String.Empty;

            // Promt the user to browse to that URL or show the Webpage in your application
            // ... 

            return Pin;
        }

        public static ImgurToken GetToken(string clientId, string clientSecret, string pin)
        {
            string Url = "https://api.imgur.com/oauth2/token/";
            string DataTemplate = "client_id={0}&client_secret={1}&grant_type=pin&pin={2}";
            string Data = String.Format(DataTemplate, clientId, clientSecret, pin);

            using(WebClient Client = new WebClient())
            {
                string ApiResponse = Client.UploadString(Url, Data);

                // Use some random JSON Parser, you´ll get access_token and refresh_token
                var Deserializer = new JavaScriptSerializer();
                var Response = Deserializer.DeserializeObject(ApiResponse) as Dictionary<string, object>;

                return new ImgurToken()
                {
                    AccessToken = Convert.ToString(Response["access_token"]),
                    RefreshToken = Convert.ToString(Response["refresh_token"])
                };
            }
        }
    }

    public class ImgurToken
    {
        public string AccessToken { get; set; }
        public string RefreshToken { get; set; }
    }
Mecaveli
  • 1,507
  • 10
  • 16