Search code examples
node.jsoauthgrant-oauth

Retrieving data from Pocket API (oAuth)


I need to retrieve my saved reading list from my Pocket account and it seems that I need to acquire access token through their oAuth to make a request.

I've got consumer key for the access token and as per Pocket API documentation, the request will resemble something like this.

POST /v3/oauth/request HTTP/1.1
Host: getpocket.com
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Accept: application/x-www-form-urlencoded

consumer_key=1234-abcd1234abcd1234abcd1234&
redirect_uri=pocketapp1234:authorizationFinished

My question is... isn't oAuth for 3rd party apps to enable authentication via Google, Facebook account? I don't see how this idea is relevant for my website that will only require access to my own data from Pocket to share on my site.

I understand I will need to authenticate somehow so I can access my data but is oAuth the process I will need to go through to get what I need?


Solution

  • It seems that they support only 3 legged OAuth flow. You can use Grant in your NodeJS app, or just get access token from here.

    Grant

    • save the following example to a file
    • set your key here: key:'...'
    • install the needed dependencies
    • run the file with node.js
    • navigate to http://localhost:3000/connect/getpocket
    • follow the instructions on screen

    At the end you'll see your access_token.

    var express = require('express')
      , session = require('express-session')
    
    var options = {
      server: {protocol:'http', host:'localhost:3000'},
      getpocket: {key:'...', callback:'/getpocket_callback'}
    }
    
    var Grant = require('grant-express')
      , grant = new Grant(options)
    
    var app = express()
    app.use(session({secret:'very secret'}))
    app.use(grant)
    
    app.get('/getpocket_callback', function (req, res) {
      console.log(req.query)
      res.end(JSON.stringify(req.query, null, 2))
    })
    
    app.listen(3000, function () {
      console.log('Express server listening on port ' + 3000)
    })
    

    }

    Purest

    Then you can use Purest to make requests to the Pocket's REST API.

    var getpocket = new Purest({provider: 'getpocket'})
    getpocket.query()
      .post('get')
      .auth('[API_KEY]', '[ACCESS_TOKEN]')
      .request(function (err, res, body) {
        // body is the parsed JSON response
      })