0

I need to send a POST request to the Instagram Basic API with Flask.

This is my code:

def get_token_insta(code):
    dict_to_send = {"client_id": "XXX",
                    "client_secret": "XXX",
                    "grant_type": "authorization_code",
                    "redirect_uri": "XXX",
                    "code": "XXX"}
    json_string = json.dumps(dict_to_send)
    res = requests.post('https://api.instagram.com/oauth/access_token', data=json_string)
    data = res.json()
    print(data)
    # More code...

The code works fine, but the response from Instagram is

{ "error_type": "OAuthException", "code": 400, "error_message": "Missing required field client_id" }

Reading the official documentation and this @murp answer I need to put data inside the body of the POST request.

Am I wrong? How can I make that work?

davidism
  • 121,510
  • 29
  • 395
  • 339
Niccolò Segato
  • 96
  • 2
  • 13

1 Answers1

2

Where did you find that you need to serialize POST request parameters to json? You'll get JSON response, sure, but you don't need to serialize request parameters. Try just to pass dict_to_send itself as data parameter:

requests.post(url, ..., data=dict_to_send)

more info in requests documentation

madbird
  • 1,326
  • 7
  • 11