Last active
March 20, 2025 07:52
Revisions
-
kemitche revised this gist
Nov 6, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ def user_agent(): Ideally, with contact info included. e.g., return "oauth2-sample-app by /u/%s" % your_reddit_username ''' raise NotImplementedError() -
kemitche revised this gist
Nov 6, 2014 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,13 @@ def user_agent(): '''reddit API clients should each have their own, unique user-agent Ideally, with contact info included. e.g., oauth2-sample-app by /u/kemitche ''' raise NotImplementedError() def base_headers(): -
kemitche revised this gist
Nov 6, 2014 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,13 @@ REDIRECT_URI = "http://localhost:65010/reddit_callback" def user_agent(): raise NotImplementedError() def base_headers(): return {"User-Agent": user_agent()} app = Flask(__name__) @app.route('/') def homepage(): @@ -58,15 +65,18 @@ def get_token(code): post_data = {"grant_type": "authorization_code", "code": code, "redirect_uri": REDIRECT_URI} headers = base_headers() response = requests.post("https://ssl.reddit.com/api/v1/access_token", auth=client_auth, headers=headers, data=post_data) token_json = response.json() return token_json["access_token"] def get_username(access_token): headers = base_headers() headers.update({"Authorization": "bearer " + access_token}) response = requests.get("https://oauth.reddit.com/api/v1/me", headers=headers) me_json = response.json() return me_json['name'] -
kemitche revised this gist
Nov 6, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -49,6 +49,8 @@ def reddit_callback(): abort(403) code = request.args.get('code') access_token = get_token(code) # Note: In most cases, you'll want to store the access token, in, say, # a session for use in other parts of your web app. return "Your reddit username is: %s" % get_username(access_token) def get_token(code): -
kemitche created this gist
Mar 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ #!/usr/bin/env python from flask import Flask, abort, request from uuid import uuid4 import requests import requests.auth import urllib CLIENT_ID = None # Fill this in with your client ID CLIENT_SECRET = None # Fill this in with your client secret REDIRECT_URI = "http://localhost:65010/reddit_callback" app = Flask(__name__) @app.route('/') def homepage(): text = '<a href="%s">Authenticate with reddit</a>' return text % make_authorization_url() def make_authorization_url(): # Generate a random string for the state parameter # Save it for use later to prevent xsrf attacks state = str(uuid4()) save_created_state(state) params = {"client_id": CLIENT_ID, "response_type": "code", "state": state, "redirect_uri": REDIRECT_URI, "duration": "temporary", "scope": "identity"} url = "https://ssl.reddit.com/api/v1/authorize?" + urllib.urlencode(params) return url # Left as an exercise to the reader. # You may want to store valid states in a database or memcache. def save_created_state(state): pass def is_valid_state(state): return True @app.route('/reddit_callback') def reddit_callback(): error = request.args.get('error', '') if error: return "Error: " + error state = request.args.get('state', '') if not is_valid_state(state): # Uh-oh, this request wasn't started by us! abort(403) code = request.args.get('code') access_token = get_token(code) return "Your reddit username is: %s" % get_username(access_token) def get_token(code): client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET) post_data = {"grant_type": "authorization_code", "code": code, "redirect_uri": REDIRECT_URI} response = requests.post("https://ssl.reddit.com/api/v1/access_token", auth=client_auth, data=post_data) token_json = response.json() return token_json["access_token"] def get_username(access_token): headers = {"Authorization": "bearer " + access_token} response = requests.get("https://oauth.reddit.com/api/v1/me", headers=headers) me_json = response.json() return me_json['name'] if __name__ == '__main__': app.run(debug=True, port=65010)