我刚开始学习Python并尝试获取Instagram的访问令牌。 以下是我写的代码。
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
因此,代码的第一部分是我将授权URL与我的客户端ID连接并重定向uri。然后,我打开浏览器并粘贴它,页面被重定向,最后我得到了代码部分。
然后我更换代码'部分与我获得的实际代码。 通过这个程序,整个过程很好。但是,如果我在不更改'代码的情况下再次运行此代码,则会显示" OAuth2AuthExchangeError:未找到匹配代码或已使用匹配代码"
我试图获取重定向的url并从中解析代码;但是,我无法使用请求包重定向网址。
有什么方法可以轻松获取代码吗?或者我每次都必须使用浏览器来获取代码?