Search code examples
pythonoauth-2.0reddit

Authorizing with Reddit API, Can't exchange code for bearer token, invalid grant?


I'm following the documentation from reddit, I get get an authorization code, but when I try to exchange it for a bearer token I get the error invalid_grant even though i've set it to the only option I can use. I'd prefer to use this method of authorization so I don't have to interact with the users credentials.

from flask import Flask, redirect, request
import requests
import requests.auth
import json
import urllib


app = Flask(__name__)


@app.route('/')
def main():
    return "<a href='/authorize'>Authorize</a>"


@app.route("/authorize")
def authorize():
    params = {
        "client_id": "client id",
        "response_type": "code",
        "state": "randomStateKey",
        "redirect_uri": "http://127.0.0.1:5000/callback",
        "duration": "temporary",
        "scope": "edit",
    }
    url = "https://www.reddit.com/api/v1/authorize?"
    return redirect(url + urllib.parse.urlencode(params))


@app.route("/callback")
def callback():
    authorization_code = request.args.get("code")
    response = code_token_exchange(authorization_code)
    return json.dumps(response)


def code_token_exchange(authorization_code):
    client_auth = requests.auth.HTTPBasicAuth('client id', 'client secret')
    headers = {"User-Agent": "MyApp/v1"}
    data = {
        "grant_type": "authorization_code",
        "code": authorization_code,
        "redirect_uri": "127.0.0.1:5000/callback"
    }
    url = "https://www.reddit.com/api/v1/access_token"
    response = requests.post(url=url, data=data, auth=client_auth, headers=headers)
    return response.json()


if __name__ == '__main__':
    app.run()


Solution

  • Such a stupid mistake, but incase anyone stumbles into the same issue:

    "redirect_uri": "127.0.0.1:5000/callback"
    

    I was missing the "http://" at the start. Ensure this matches the redirect url in your reddit application account.