Search code examples
jsondjangopython-requestsinstagram-api

Requests for Instagram Access token


I am working on Instagram API in Django(python)

I am getting code from

'https://api.instagram.com/oauth/authorize/?client_id=%s&response_type=code&redirect_uri=%s' % (INSTAGRAM_APP_CONSUMER_KEY, redirect_uri)

but when i am exchanging code for access token code is failing

# All arguments are valid 
def execute(self, code, redirect_uri, app_id, app_secret):
    exchange_url = 'https://api.instagram.com/oauth/access_token'
    try:
        #Update :  get request to post
        r = requests.post(exchange_url, params={
            'client_id': app_id,
            'redirect_uri': redirect_uri,
            'client_secret': app_secret,
            'code': code,
            'grant_type': 'authorization_code'

        })
        #print(r)
        #print(json.loads(r))
        print(r.json())
        return r.json()
    except Exception as e:
        print(e)

r.json() gives simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1

Update 1 : r.json() works after request changed from get to post but error message comming 'You must provide a client_id'

Please let me know what i am doing wrong


Solution

  • I think it requires data in post data, not in query params.
    Try this :

    your_post_data = {'client_id': '', ... }
    r = requests.post('your_url', data=your_post_data)  
    

    Ref: Python Requests Docs