Save URL to Pocket with Alfred workflow using oAuth

Written by Alex Wolkov

I’ve finished another workflow, and this one is a bit special. 

Post links to Pocket directly from Alfred. You can download it here and the code is on github , feel free to submit pull requests.

Usage :

First login with pocket using “pocket_login” command, this will take you to the pocket site to atuhenticate.

Screen Shot 2013-03-29 at 8.50.43 PM

Then use “pocket” keyword, to post urls from Chrome/Safari/Clipboard straight to pocket.

Screen Shot 2013-03-29 at 9.09.25 PM

There were already some nice pocket workflows, but they all required either the official Pocket.app for mac or another form of username/password authentication.

I didn’t really wanted to store my Pocket passwords, so I created this workflow using Pocket API and oAuth.

Doing oAuth with Alfred is a bit tricky. But manageable.

The way I found that works is as follows :

  • When user writes “pocket_login”, a ruby script runs via script filter, which requests the initial token from pocket.
  • After the token was returned, it’s saved to a file and the script returns an option to the user to login
  • Then 2 things happen
  • Alfred opens the Pocket oAuth url in the users default browser, with the “success_url” of localhost:2222
  • Another ruby script runs, which opens a server that listens on port 2222 on the localhost
  • When user has completed the auth flow in Pocket site, he then is redirected to the ruby server
  • When the server detects a connection, it completes the last step of the flow, which is converting the token to access_token with which we can call any Pocket API methods securely. And then saves that access_token to a file

Now I know this may seem a little hacky, but it works perfectly, and the server even has a timeout, so it won’t run forever and to minimize security risks.

The code was written in ruby, as I found out that it’s the simplest language to allow me to run a webserver from the command line without additional gems/packages what have you (php didn’t work for some reason :/).

And I must admit, the code is quite messy, because I’m a ruby noob and I was learning as I go.