Skip to content

Instantly share code, notes, and snippets.

@hyperdefined
Last active January 14, 2025 11:49
Show Gist options
  • Save hyperdefined/6ae12adde7f8e853a56c54e98a9a6ea9 to your computer and use it in GitHub Desktop.
Save hyperdefined/6ae12adde7f8e853a56c54e98a9a6ea9 to your computer and use it in GitHub Desktop.

How to setup reddit for a cobalt instance

Step 1

Create an app here: https://www.reddit.com/prefs/apps image

The information can be anything. Make sure to set it to web app and redirect uri to http://localhost:8080

Step 2

Copy the client ID and client secret tokens.

image

You will need python3. Run pip3 install praw. Afterwards, run the python script attached. Replace the ID and secret with your own.

python3 refresh_token.py <client_id> <client_secret>

Step 4

You will get an output if everything works like this:

Now open this url in your browser: <url>

Open the URL, and you should see something like this: image

Scroll down and click "allow."

Step 5

Afterwards, your browser will display the redirect token. You need the 3 keys for your instance (refresh, client id/secret).

You can now setup cookies on your instance. Create a cookies.json file and place it right next to your docker-compose.yml.

{
    "reddit": [
        "client_id=<replace_this>; client_secret=<replace_this>; refresh_token=<replace_this>"
    ]
}

Simply replace the tokens with your own. You don't need to add quotes.

Step 6

In your docker-compose.yml you want to add this under cobalt-api:

volumes:
    - ./cookies.json:/cookies.json

Lastly, add COOKIE_PATH: "/cookies.json" under environment. Restart your container, and you're all set!

#!/usr/bin/env python
"""This example demonstrates the flow for retrieving a refresh token.
This tool can be used to conveniently create refresh tokens for later use with your web
application OAuth2 credentials.
To create a Reddit application visit the following link while logged into the account
you want to create a refresh token for: https://www.reddit.com/prefs/apps/
Create a "web app" with the redirect uri set to: http://localhost:8080
After the application is created, take note of:
- REDDIT_CLIENT_ID; the line just under "web app" in the upper left of the Reddit
Application
- REDDIT_CLIENT_SECRET; the value to the right of "secret"
Usage:
EXPORT praw_client_id=<REDDIT_CLIENT_ID>
EXPORT praw_client_secret=<REDDIT_CLIENT_SECRET>
python3 obtain_refresh_token.py
"""
import random
import socket
import sys
import praw
def main():
"""Provide the program's entry point when directly executed."""
scopes = "*"
client_id = sys.argv[1]
client_secret = sys.argv[2]
reddit = praw.Reddit(
redirect_uri="http://localhost:8080",
user_agent="obtain_refresh_token/v0 by u/bboe",
client_id=client_id,
client_secret=client_secret
)
state = str(random.randint(0, 65000))
url = reddit.auth.url(duration="permanent", scopes=scopes, state=state)
print(f"Now open this url in your browser: {url}")
client = receive_connection()
data = client.recv(1024).decode("utf-8")
param_tokens = data.split(" ", 2)[1].split("?", 1)[1].split("&")
params = dict([token.split("=") for token in param_tokens])
if state != params["state"]:
send_message(
client,
f"State mismatch. Expected: {state} Received: {params['state']}",
)
return 1
elif "error" in params:
send_message(client, params["error"])
return 1
refresh_token = reddit.auth.authorize(params["code"])
send_message(client, f"Refresh token: {refresh_token}")
return 0
def receive_connection():
"""Wait for and then return a connected socket..
Opens a TCP connection on port 8080, and waits for a single client.
"""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("localhost", 8080))
server.listen(1)
client = server.accept()[0]
server.close()
return client
def send_message(client, message):
"""Send message to client and close the connection."""
print(message)
client.send(f"HTTP/1.1 200 OK\r\n\r\n{message}".encode())
client.close()
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment