Aren’t there existing wrappers in Python?

Yes. There are few existing wrappers in python for Pocket API like:

But found them daunting when trying to understand a simple implementation of an API.

Why this ?

This is a very basic tutorial. The intend is not to develop a production ready code; but something which will help you understand how the pocket api works. This is very low level and is for someone who is just starting using an API.

Pocket API Authentication

This tutorial is aligned with the official documentation. Hence, it should read along official documents at: Pocket Authentication Documents

The initial step involves authentication and we will see how to implement the steps given in pocket documentation.

####Step 1: Obtain a platform consumer key

Registering your app with Pocket associates it with a platform consumer key. This key identifies your app to Pocket’s API.

If you have not obtained a consumer key yet, you can register for one at http://getpocket.com/developer/apps/new.

Create Application

After clicking on Create an Application, you are prompted to enter few details:

Create Application

After registering, you should get a consumer key such as below: Create Application

####Step 2: Obtain a request token The real fun starts now. Let’s dive into some python code. We will use requests - a simple HTTP library for Python which is built for human beings. If it is not installed, please install using:

 pip install requests 

Once you have requests installed, it is time to use it.

1
2
3
4
5
6
7
8
9
10
11
12
  >>> import requests
  #connecting to pocket API; pocket_api stores the http response
  >>> pocket_api = requests.post('https://getpocket.com/v3/oauth/request',
         data = {'consumer_key':'51652-157f3ac6baf0913393bf65c2',
                 'redirect_uri':'https://saideepchandg.github.io'})

  >>> pocket_api.status_code       #if 200, it means all ok.
  200
  >>> pocket_api.headers           #prints in JSON format
  {'Status': '200 OK', 'Content-Length': '556', 'X-Limit-Key-Limit': '10000', 'X-Limit-User-Limit': '500', 'X-Source': 'Pocket', 'Set-Cookie': 'PHPSESSID=j1dffo1fqilmk11933qhqroqu4; path=/', 'X-Limit-Key-Remaining': '9999', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Server': 'Apache', 'X-Limit-User-Reset': '3600', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Sun, 21 Feb 2016 10:22:03 GMT', 'P3P': 'policyref="/w3c/p3p.xml", CP="ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE"', 'Content-Type': 'application/json', 'X-Limit-Key-Reset': '3600', 'X-Limit-User-Remaining': '499'}

  >>> print(pocket_api.headers['X-Error'])#gives error reason 
Notes:
  • the redirect_uri is used by pocket to redirect the user once the authentication is completed. Other than that it has no significance. If this example we can replace it with something unrelated like www.google.com
  • All we wanted was the request token. This is also part of the http response stored in pocket_api.text. And it can be accessed using:
1
2
  >>> pocket_api.text
u'code=60c51100-4d50-25ee-d412-84456e' 

Step 3: Redirect user to Pocket to continue authorization

This step needs to be done in browser as Pocket need to authenticate. In real life your application should automatically build the url and direct the user to this URL so that Pocket can authenticate. For this exercise, copy below url and paste in browser:

https://getpocket.com/auth/authorize?request_token=60c51100-4d50-25ee-d412-84456e&redirect_uri=https://saideepchandg.github.io Authorize User

Step 4: Receive the callback from Pocket

Once you click on Authorize button, Pocket re-directs user to the link we gave in redirect_uri.

Step 5: Convert a request token into a Pocket access token

We will use requests to get the access token.

1
2
3
4
5
6
7
8
9
>>> import requests
>>> pocket_auth = requests.post('https://getpocket.com/v3/oauth/authorize',
       data = {'consumer_key':'51652-157f3ac6baf0913393bf65c2',
               'code': '60c51100-4d50-25ee-d412-84456e'})

>>> pocket_auth.status_code
200                         # 200 - means our request was ok.
>>> pocket_auth.text
u'access_token=8db1fd53-e448-17de-ba55-3ef351&username=pocket.api.demo%40gmail.com'

What next?

Hurray!! Now we have all requisite codes to access the Pocket account of a user:

‘consumer_key’ : ‘51652-157f3ac6baf0913393bf65c2’
‘access_token’ : ‘8db1fd53-e448-17de-ba55-3ef351’


Adding article can be done using add api.

1
2
3
4
5
6
>>> pocket_add = requests.post('https://getpocket.com/v3/add',
       data= {'url': 'http://www.brainyquote.com/quotes/quotes/b/benjaminfr383997',
              'consumer_key':'51652-157f3ac6baf0913393bf65c2',
              'access_token': '8db1fd53-e448-17de-ba55-3ef351'})
>>> pocket_add.status_code
200

After this if you go to the user’s home page, it should show the page that we added using pocket API
User Home Page after adding

Wasn’t this easy?