Sunday, January 13, 2013

Upload images to your imgur account from the command line

If you just want to anonymously do a command line upload to imgur, use Bart Nagel's fairly complete, available along with many other tools on imgur's apps page. To make it work on my Mac I installed GNU sed with brew install gnu-sed and changed sed to gsed in the script, and it works fine, but it doesn't connect to your imgur account.

Another approach is to pull out an imgur cookie from a browser session and then sort of just pretend to be that browser session from some programming language. Lance Pollard wrote up something about this for Ruby. It might work. To me, it's a pretty ugly way to go about it.

I had never really done anything with OAuth2, so I felt pretty victorious when I got this to work. Alternate title: "Five minutes to OAuth2 uploads on imgur". I presume that you already have an imgur account for the following.

Step One: Register an application. The application is really just you, but that's fine. Choose "Desktop" for "Application Type". You should quickly get to a success page that gives you a "Client ID" and "Client secret". These are just strings that identify you; use them in the all-caps spots in subsequent steps.

Step Two: In a browser, go to this URL:
This is where you log in, convincing imgur that you are really you. You should quickly get to another success page that gives you a new string, this one being a "PIN".

Step Three: At the command line, enter this:
curl -X POST -F "client_id=YOUR_CLIENT_ID" -F "client_secret=YOUR_CLIENT_SECRET" -F "grant_type=pin" -F "pin=YOUR_PIN"
This will get you a JSON response which will include "access_token", which is the useful bit. You might also be interested in "refresh_token", but I'm not going there in this post.

Step Final: At the command line, enter this:
curl -X POST -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -F "image=@PATH_TO_YOUR_IMAGE_FILE"
If cURL is talking about not being able to access your file, mess around with un-escaping spaces and stuff like that. It'll work. And this time you'll get a nice JSON response which includes an "id" and a "link" to your newly uploaded file - and that file will also now be appearing on your imgur account images page! This is especially good if you have a pro account and want your images to stay up indefinitely.

That's it! You can probably automate this further if you want. If you're building a web app, there are other better ways to interface with OAuth2. You can check the whole imgur API documentation.


Andy said...

Step 3 is not working for me. I supplied the correct Client ID, secret client service ID, and PIN, and the response I get is:

{"data":{"error":"Authentication required","request":"\/3\/upload","method":"GET"},"success":false,"status":401}

I'm wondering if something has changed with the API since you made this post. Any help would be appreciated. I am not really a programmer, though I have a fair amount of bash experience.

Unknown said...

Hi Andy! Very possible the API has changed; I'm not sure. You might have better luck with something other than imgur anyway. Good luck!

Dan Jones said...

This is great. I hacked together a bash script based on your post:

Just requires bash, curl, and jshon