Skip to content

Instantly share code, notes, and snippets.

@bboe
Created June 5, 2015 05:49
Show Gist options
  • Save bboe/670d139037c3eec554eb to your computer and use it in GitHub Desktop.
Save bboe/670d139037c3eec554eb to your computer and use it in GitHub Desktop.
Create permanent reddit oauth tokens
#!/usr/bin/env python
"""Script to generate permenant OAuth tokens for the desired reddit scope."""
from __future__ import print_function
import os
import praw
import sys
from pprint import pprint
def prompt(msg):
sys.stdout.write(msg)
sys.stdout.flush()
return sys.stdin.readline().strip()
def main():
if len(sys.argv) != 5:
print('Usage: {0} CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, SCOPE'
.format(os.path.basename(sys.argv[0])))
return 1
keys = 'oauth_client_id oauth_client_secret oauth_redirect_uri'.split()
r = praw.Reddit('reddit oauth token fetch',
**dict(zip(keys, sys.argv[1:5])))
scope = sys.argv[-1]
print('Requesting `{0}` scope'.format(scope))
print('Visit: {0}'.format(r.get_authorize_url('...', scope, True)))
pprint(r.get_access_information(prompt('Code: ')))
return 0
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit(1)
@geirawsm
Copy link

geirawsm commented Jul 9, 2015

Hey and thanks for the code. I took the liberty of shamelessly copying this into my own repo for my redditbot (ligabot/oauth_permanent_token.py) and credited you. I hope this is ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment