سؤال

I'm trying to get a simple bash script working with the pocket api. All I want to do is authenticate with pocket and download my list of articles (actually, only the count)

I'm a little confused by the way that the oauth process works.

  • I've registered my app with the pocket api and have a consumer key
  • its marked as in "development" - I'm not sure if this is important.

The bit that's confusing me is that it seems that the way that the oAuth flow works with it's redirect uris is that it only really works with a gui (i.e a browser) - is it possible to do this with a bash script?

Here is what I have below. it works up until I have the TOKEN, but then I'm not sure what to do next.

    #!/bin/bash

    REDIR="redirect_uri=pocketapp1234:authorizationFinished"
    KEY=21004-xxxxxxabcabcabc # you can assume this is the consumer key pocket issues for my app.
    CODE=`curl -X POST --data "consumer_key=$KEY&$REDIR" https://getpocket.com/v3/oauth/request`

    echo "OK - code is $CODE"
    TOKEN=$(echo $CODE | awk -F"=" '{print $2}')
    echo "OK - token is $TOKEN"

    AUTH="consumer_key=$KEY&$CODE"


# This line seems not to work
     curl -v  "https://getpocket.com/auth/authorize?request_token=$TOKEN&$REDIR"
هل كانت مفيدة؟

المحلول

Yes, the browser portion is required. At the authorize phase, there is a page from getpocket.com prompting the user to login and authorise the bash script to access the user's Pocket account.

You can refer to Step 3 of the Pocket API Docs.

نصائح أخرى

This is the Python v3.8 script I'm using and seems to work.

#!/usr/bin/env python
from os import environ as env
import requests
import webbrowser


def authorize_pocket_app():
    data = {
        "consumer_key": env['POCKET_CONSUMER_KEY'],
        "redirect_uri": env['POCKET_APP_NAME'],
    }
    resp = requests.post(url="https://getpocket.com/v3/oauth/request", data=data)
    code = resp.text.split("=")[1]
    webbrowser.open(f"https://getpocket.com/auth/authorize?request_token={code}"
                    "&redirect_uri=https://duckduckgo.com")
    input("Authorize %s app in the browser, then click enter" % env['POCKET_APP_NAME'])
    get_token(code)


def get_token(code):
    resp = requests.post(
        url="https://getpocket.com/v3/oauth/authorize",
        data={
            "consumer_key": env["POCKET_CONSUMER_KEY"],
            "code": code,
        })

    token = resp.text.split("&")[0].split("=")[1]
    print("Secret token:", token)


if __name__ == "__main__":
    authorize_pocket_app()

To use it as is you need to install requests external library and export POCKET_CONSUMER_KEY and POCKET_APP_NAME in your shell environment. E.g.

pip install requests
export POCKET_CONSUMER_KEY=xxx-yyy-zzz
export POCKET_APP_NAME=my-pocket-app
python <filename>.py

HTH

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top