Skip to content

Instantly share code, notes, and snippets.

Last active September 1, 2022 06:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/cfbec5ee35738ee2d5f6459eb92b1925 to your computer and use it in GitHub Desktop.
Save simonw/cfbec5ee35738ee2d5f6459eb92b1925 to your computer and use it in GitHub Desktop.
"install": "pip3 install --user -r requirements.txt",
"start": "PYTHONUNBUFFERED=true python3",
"watch": {
"ignore": [
"install": {
"include": [
"restart": {
"include": [
"throttle": 1000
from flask import Flask, request, redirect, session
import requests
import urllib.parse
import os
STYLE = "<style>body { font-family: Helvetica, sans-serif; }</style>"
app = Flask(__name__)
app.secret_key = SESSION_SECRET
def hello():
return """
<title>Get your own Pocket OAuth token</title>
<h1>Get your own Pocket OAuth token</h1>
<p>You need to first create a new application with a Consumer Key.</p>
<p>You can do that using Pocket's <a href="">Create an application</a> form.</p>
<form action="/start" method="post">
<p><label>Your consumer key: <input type="text" name="consumer_key" value="{consumer_key}"></label></p>
<p><input type="submit" value="Get a personal access token for your consumer key" style="font-size: 1.2em; background-color: #ccc; border: 1px solid black; padding: 0.2em 0.6em"></p>
<p><small><a href="">Source code for this tool</a></p>
consumer_key=session.get("consumer_key", ""),
@app.route("/start", methods=["POST"])
def start():
# Need to POST to Pocket to get a request token
consumer_key = request.form.get("consumer_key", "")
if not consumer_key:
return redirect("/", code=302)
response =
"consumer_key": consumer_key,
"redirect_uri": REDIRECT_URI,
assert response.text.startswith("code=")
request_token = response.text[len("code=") :]
# When we redirect back we will need the consumer_key and request_token
session["consumer_key"] = consumer_key
session["request_token"] = request_token
# Now send them to the Pocket site to sign in
redirect_to = "{}&redirect_uri={}".format(
request_token, REDIRECT_URI
return redirect(redirect_to, code=302)
def auth():
consumer_key = session["consumer_key"]
response =
"consumer_key": consumer_key,
"code": session["request_token"],
session.pop("request_token", None)
codes_raw = response.text
codes = dict(urllib.parse.parse_qsl(codes_raw))
codes["consumer_key"] = consumer_key
codes["style"] = STYLE
] = "{}&access_token={}".format(
consumer_key, codes["access_token"]
return """
<title>Your Pocket tokens</title>
<h1>Your Pocket tokens</h1>
<pre>Consumer key: {consumer_key}\nAccess token: {access_token}\nUsername: {username}</pre>
<p>Treat the access token carefully, as if it was your password.</p>
<p>Try out the API: <a href="{demo}">{demo}</a>
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment