If you just want to anonymously do a command line upload to imgur, use Bart Nagel's fairly complete imgurbash.sh, 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:
https://api.imgur.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=pin
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" https://api.imgur.com/oauth2/token
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" https://api.imgur.com/3/upload
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.
Subscribe to:
Post Comments (Atom)
3 comments:
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.
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!
This is great. I hacked together a bash script based on your post: https://gist.github.com/goodevilgenius/92de52472c97532034a34fa3402f2aa0
Just requires bash, curl, and jshon
Post a Comment