Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Requests : How to get response cookie from 302 redirect

I'm trying to make an automation that will send the http requests to follow or unfollow a user through an instagram api, right now I'm using the Python Requests module and Im trying to do it through the site 'http://pikore.com'. My current code is is :

import requests
from requests.auth import HTTPBasicAuth

s = requests.Session()
s.get('http://pikore.com')

print(s.cookies)
s.get('http://www.pikore.com/session/new?from=%2F', auth=HTTPBasicAuth('USERNAME', 'USERSECRET'))
pikore_session = s.cookies['_pikore_session']
print(s.cookies)
s.get('http://pikore.com')
print(s.cookies)

cookies = {
'_pikore_session': pikore_session,
'token': 'BAhJIjcyNTY5NDczOTIuZWIxM2FlYi41Mjc3ZTI4NjI4ZDM0NDRlOGRjNWNjZmFjNWU3YmJmYgY6BkVU--9b52c6305368f28c83ffc288e9d55e94b3e864db',
}

headers = {
    'Host': 'www.pikore.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
    'Referer': 'http://www.pikore.com/nike',
}

print(s.get('http://www.pikore.com/follow/13460080', headers=headers, cookies=cookies))
s.get('http://www.pikore.com/')
print(s.cookies)

So this works and will carry out the given request, the only thing is, the authorization line will only get the '_pikore_session' cookie, but not the token cookie, which I want to get. After you log in through the instagram authorization redirect, it will redirect you three times and then finally you'll land on the website, logged in, but on the third redirect, I can see that it outputs the 'token' response cookie, I want to someone get that so that I dont have to go and get it each time manually.

like image 549
Fuledbyramen Avatar asked Dec 10 '22 17:12

Fuledbyramen


2 Answers

I was trying to solve this today and found a relevant bug in requests.

Using JohnCC330's solution of turning off auto_redirect worked for me:

res = requests.post(
    host,
    data={'somefield':'my value'},
    allow_redirects=False)

if res.status_code == 302: # expected here
    jar = res.cookies
    redirect_URL2 = res.headers['Location']
    res2 = requests.get(redirect_URL2, cookies=jar)
    # res2 is made with cookies collected during res' 302 redirect

Hope this helps,

Bob

like image 93
BobRz Avatar answered Jan 04 '23 03:01

BobRz


Alternatively you can keep the automatic redirect support and iterate through the response history to merge the cookies. This code will return all cookies as a dict. This includes cookies that were only returned in the redirect.

def get_cookies_from_response(response: requests.Response) -> dict:
    """Given a response, return all cookies as a dict."""
    cookies = response.cookies.get_dict()
    # If there was a redirect, the cookies we need may be in a previous response
    for r in response.history:
        cookies.update(r.cookies.get_dict())  # Merge cookies
        
    return cookies

res = requests.post(
    host,
    data={'somefield':'my value'},
    allow_redirects=True)

cookies = get_cookies_from_response(res)
like image 36
Russell Avatar answered Jan 04 '23 05:01

Russell