Forums

Instagram OAuth problem

Hi, I have set up a very basic Flask page with Instagram OAuth2. On the instagram developer page, it states that in order to get an acces token, the final step should be to post a curl -F etc. request to the server, which I wanted to solve with the requests.post() function, but somehow I get an error. Here is my code:

from flask import Flask, request, redirect
import requests

client_id = ""
client_secret = ""
redirect_uri = "http://ecokarst.pythonanywhere.com/callback"
authorization_url = "https://api.instagram.com/oauth/authorize"

app = Flask(__name__)
app.debug = True
app.secret_key = 'development'

@app.route('/')
def index():
    return redirect(authorization_url + "/?client_id=" + client_id + "&redirect_uri=" + redirect_uri + "&scope=public_content&response_type=code")

@app.route('/callback')
def callback():
    code = request.args.get('token')

    payload = {"client_id": client_id,
    "client_secret": client_secret,
    "grant_type": "authorization_code",
    "redirect_uri": redirect_uri,
    "code": code}

    r = requests.post('https://api.instagram.com/oauth/access_token', json=payload)
    return r.text

And I get an error at the last step:

{"error_type": "OAuthException", "code": 400, "error_message": "You must provide a client_id"}

As you have seen, I did post a client_id which must be good, as without a good client_id I could not reach my callback page and should get an error earlier. I assume, that my requests.post() function has some problem, but cannot find the error. Any idea? Is there a better way to apply a curl -F POST request in Python? Thanks.

The solution is that I do not have to post a json but a dictionary:

r = requests.post('https://api.instagram.com/oauth/access_token', data=payload)

This way it worked well.

Excellent -- glad you worked it out!