在Python上获取Instagram的访问令牌

2024-03-29 09:38:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学习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。然后,我打开我的浏览器并粘贴它,页面被重定向,最后得到代码部分。在

然后用我获得的代码替换。 有了这个程序,整个过程都很好。但是,如果我在不更改“code”的情况下再次运行此程序,它将显示“OAuth2AuthExchangeError:匹配代码未找到或已被使用”

我试图获取重定向的url并从中解析代码;但是,我无法通过requests包获得重定向url。在

有什么方法可以让我轻松地得到代码吗?还是每次都要在浏览器上获取代码?在


Tags: the代码clienttokenapiidurlaccess