2

I just started learning Python and trying to get access token for Instagram. And below is the code that I wrote.

from instagram.client import InstagramAPI

service_url = 'https://api.instagram.com/oauth/authorize/client_id='+clientid+'&redirect_uri='+re_url+'&response_type=code'

print service_url

code = 'code' #I open the browser and go on to the service url and get the code

api = InstagramAPI(client_id=clientid, client_secret=clientsecret, redirect_uri=re_url)

access_token = api.exchange_code_for_access_token('code')

print access_token

So the first part of the codes is that I concatenate authorization url with my client id and redirecting uri. Then, I open my browser and paste it, and the page is redirected, and finally I get the code part.

Then I replace 'code' part with actual code that I acquired. With this procedure the whole thing works fine. However,if I run this again without changing the 'code', it says "OAuth2AuthExchangeError: Matching code was not found or was already used"

I tried to get redirected url and parse the code from it; however, I can not get redirected url with requests package.

Is there any way that I can get code easily? or do I have to go on a browser every time to get a code?

Szabolcs
  • 3,990
  • 18
  • 38
Ray
  • 383
  • 1
  • 2
  • 9
  • You would receive the access token one time. https://github.com/facebookarchive/python-instagram/blob/master/get_access_token.py – FabricioG Nov 30 '18 at 23:17

0 Answers0