1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
import rauth, rauth.service, base64, os, sys

reddit = rauth.service.OAuth2Service(
        name="reddit",
        consumer_key='cust-key',
        consumer_secret='Secret',
        access_token_url="https://ssl.reddit.com/api/v1/access_token",
        authorize_url="https://ssl.reddit.com/api/v1/authorize")

print "Content-Type: text/html;charset=utf-8"
print

try:
    KEY = int(os.environ["REMOTE_ADDR"].replace(".",""))
    GET = dict((k.strip(), v.strip()) for k,v in (item.split('=') for item in os.environ["QUERY_STRING"].split('&')))
    print "-" + GET["code"] + "-<br />"
    reddit_url = reddit.get_authorize_url(
            response_type="code",
            scope="identity",
            state=base64.b64encode(os.urandom(32)),
            redirect_uri='http://www.redirect')
    
    print "<a href='%s'>wee</a><br />" % reddit_url
    
except:
    reddit_url = reddit.get_authorize_url(
            response_type="code",
            scope="identity",
            state=base64.b64encode(os.urandom(32)),
            redirect_uri='http://www.redirect')
    
    print "<a href='%s'>wee</a>" % reddit_url
    sys.exit()
    
print "logging in<br />"

# when user is redirected back after authorizing:
code = GET["code"]
response = reddit.get_access_token(
    auth=(reddit.consumer_key, reddit.consumer_secret),
    data=dict(
        grant_type="authorization_code",
        code=code,
        redirect_uri='http://www.redirect'))
access_token = response.content
#["access_token"]
print access_token