1

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?

simo
  • 15,078
  • 7
  • 45
  • 59
Seong Lee
  • 10,314
  • 25
  • 68
  • 106

2 Answers2

3

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
  })
simo
  • 15,078
  • 7
  • 45
  • 59
  • I didn't know any of these. I will try them now, thanks a lot! – Seong Lee Oct 07 '15 at 08:18
  • On Grant website, what do I need to put in the secret field? – Seong Lee Oct 07 '15 at 08:26
  • That's a good question. Pocket doesn't seems to have a secret, so therefore this UI won't let you use your own app credentials. Pocket actually deviates from the OAuth spec quite a lot, so that's the reason why that's not implemented. However you can get Grant running quite easily on your own localhost, and authenticate from there. You need to set only the `key` in your configuration. – simo Oct 07 '15 at 08:47
  • I found a great tutorial on using Grant https://scotch.io/tutorials/implement-oauth-into-your-express-koa-or-hapi-applications-using-grant but it doesn't cover the scenario where I won't have 'secret'. Can you please point me in some direction? – Seong Lee Oct 07 '15 at 13:14
  • Updated my answer above, take a look at it, and follow the instructions. – simo Oct 07 '15 at 14:45
  • This is great. I've run Grant successfully and got my access token. And also got my JSON data using Purest! By the way, will I no longer need Grant once I got the token? And I was wondering why installing Grant and Purest comes packaged with node_modules folder with qs module in it? I installed qs in my app and I'm seeing copies of qs modules in your modules so I was curious why that is? – Seong Lee Oct 07 '15 at 22:38
  • I have another question about query API of Purest http://stackoverflow.com/questions/33003850/how-to-query-with-parameters-using-purest-module-node-js Maybe you're the best person to ask. – Seong Lee Oct 08 '15 at 01:09
  • The Pocket's `access_token` seems to be non expiring, so you can use it for as long as you want. I posted an answer to your other question regarding Purest. – simo Oct 08 '15 at 06:12
  • so in case of non-expiring, I won't need Grant in my codebase, right? – Seong Lee Oct 08 '15 at 07:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91714/discussion-between-simo-and-seong-lee). – simo Oct 08 '15 at 08:59
1

For anyone reading this in 2021 or later, wanting to make a simple script to add articles to their pocket, I came up with this:

1: get your consumer key, via pocket's site.

2: get you access token, using this tool it's very simple. If you want to make an app or something that'll work without it, I guess the above (old) answer might work, didn't test it.

3: Use the following code to add an article:

var request = require('request');

request.post({
        url: 'https://getpocket.com/v3/add',
        form: {
            url: 'https://articleToAdd.com',
            consumer_key: '123456-12abcd1234a1ab12a12abc12',
            access_token: '12345a1a-1ab1-1a12-12a1-1a1234'
        }
    },
    function(err, httpResponse, body) { console.log(httpResponse.body) }
)

Hope it helps someone that is looking to do the same. Retrieving/modifying articles is similar, look here for the specifics.

Leander
  • 298
  • 2
  • 7