1

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"
4

2 回答 2

2

是的,浏览器部分是必需的。在授权阶段,getpocket.com 有一个页面提示用户登录并授权 bash 脚本访问用户的 Pocket 帐户。

您可以参考Pocket API Docs的第 3 步。

于 2014-03-29T15:38:31.023 回答
0

这是我正在使用的 Python v3.8 脚本,它似乎可以工作。

#!/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()

要按原样使用它,您需要在 shell 环境中安装外部库和requests导出。例如POCKET_CONSUMER_KEYPOCKET_APP_NAME

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

高温高压

于 2020-02-29T15:55:36.067 回答