在为 Instagram 执行 oauth2 时,存在以下形式的重定向调用:
https://api.instagram.com/oauth/authorize/?client_id=<XXX>&redirect_uri=<YYY>&response_type=code&scope=basic+likes+comments
当编码为Requests:
s = requests.Session()
###Create URL call with query parameters
user_AuthRequest_Instagram = 'https://api.instagram.com/oauth/authorize/?client_id=' + Client_ID + '&redirect_uri=' + redirect_URL + '&response_type=code&scope=basic+likes+comments'
###Get the result of this URL
r = s.get(user_AuthRequest_Instagram, allow_redirects=True, timeout=120)
###Checking results
print(r.status_code, r.url, r.cookies, r.text)
where 客户端ID and 重定向网址是适当的值。这确实会产生 200 HTTP status_code 值,但重定向永远不会作为 post 的结果被调用。同样的方法与urllib产生相同的结果;即使它返回 200 status_code,它也不会调用重定向。
采用完全相同的 URL 命令并将其放入 Chrome 中是成功的,重定向在 Instagram 上完全正常工作。我什至使用成功测试了这一点网页浏览器:
import webbrowser
b = webbrowser.get('chrome')
webbrowser.open(user_AuthRequest_Instagram)
那么为什么Requests方法对这种 URL 重定向情况不起作用吗?