简体   繁体   English

使用 python 和请求进行 Instagram 身份验证

[英]Instagram Authentification with python and requests

I need to create the instagram login form for my project.我需要为我的项目创建 Instagram 登录表单。 I've written this code but it doesnt work correctly.我写了这段代码,但它不能正常工作。 I need to get the 'sessionid' cookie after the request我需要在请求后获取“sessionid”cookie

def authorize_inst():
    url = 'https://www.instagram.com/'
    url_main = url + 'accounts/login/ajax/'
    req1 = requests.get(url)
    print(req1.headers)
    print(req1.cookies['csrftoken'])
    print('-----')
    auth = {'username':'login','password':'pass'}
    req2 = requests.post(url_main,cookies={'csrftoken':req1.cookies['csrftoken']},data=auth,allow_redirects=True)
    print(req2.headers)
    print(req2.cookies)

Here are the response headers:以下是响应头:

`{'Content-Type': 'text/html', 'X-Frame-Options': 'SAMEORIGIN', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT', 'Vary': 'Cookie, Accept-Language', 'Content-Language': 'en', 'Access-Control-Allow-Origin': 'https://www.instagram.com/', 'Date': 'Sat, 17 Feb 2018 08:46:12 GMT', 'Strict-Transport-Security': 'max-age=86400', 'Set-Cookie': 'rur=FTW; Path=/, csrftoken=KSGEZBQrpQBQ8srEcK98teilzOsndDcF; expires=Sat, 16-Feb-2019 08:46:12 GMT; Max-Age=31449600; Path=/; Secure, mid=Wofr1AAEAAGPK-9pKoyWokm4jRo8; expires=Fri, 12-Feb-2038 08:46:12 GMT; Max-Age=630720000; Path=/', 'Connection': 'keep-alive', 'Content-Length': '21191'`}

And a part code from req2.content :以及来自req2.content的部分代码:

<title>\n                  Page Not Found &bull; Instagram\n                </title>

What is the problem?问题是什么? Thank you in advance.先感谢您。

I've found the decision.我找到了决定。 Here is the code:这是代码:

import requests

url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
req1 = requests.get(url)
auth = {'username': 'login', 'password': 'pass'}
headers = {
    'origin': "https://www.instagram.com",
    'x-instagram-ajax': "1",
    'content-type': "application/x-www-form-urlencoded",
    'accept': "*/*",
    'x-requested-with': "XMLHttpRequest",
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36",
    'x-csrftoken': req1.cookies['csrftoken'],
    'referer': "https://www.instagram.com/accounts/login/",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
    'cookie': "fbm_124024574287414=base_domain=.instagram.com; ig_or=landscape-primary; ig_pr=1; ig_vw=1920; mid=Wcp3vAALAAGjSka8GEnPwijnia6-; rur=FTW; ig_vh=949; csrftoken="+req1.cookies['csrftoken']+"; fbsr_124024574287414=jSVsTpoGuOgZQB0vEP_X70hrO2-LlfD9EnUz9nwGTXo.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUMyM1FOT2ZwQU1oRVVudldzeGp1dHpwckEyODBLbUZseVo4VjVMMVVRVkJYbUVadHFyd25nekdtdzg2ejFTajRIYzVSWVRISHlvTjZXU29ScEdDZXB5RnRTMDloRXlLT3dXbU5uTTloQV9PTFE2VUI2ZFhPWW5Qa3pBNlNSZkFpSWZiU2N2anEyRFZna2FMdkdDWkRBQklCbElhYVAya2JNZzJBQW9fU0lzS3Z5NDhHRXB2RjFwQmdKOHNrdjltZWtYbFF1Z1dib040UXlzM2lwUTVfRUsxTjJUaHBpb3g1QUF2SDNpSVE2Qm1fdTFSeTZTVHFZMWR1M2NCSU5FRHpiZXRaRjFvSXY1MGJzLWFWQk4tcmFsVHY1dGE2VS13ajZCUXE0UlFEQjVHZEdqeDZpZkdlc0JsYnZvQUNlVFFJQ3pVSl9id1F1eGpyc0UxbEFzalRWZCIsImlzc3VlZF9hdCI6MTUxODg4NDA1MCwidXNlcl9pZCI6IjEwMDAyMzcyMDI5NTQyNyJ9",
    'x-compress': "null",
    'cache-control': "no-cache"
}
req2 = requests.post(url_main,data=auth,headers=headers)
sessionid = req2.cookies['sessionid']

print(req2.content)
print(req2.cookies['ds_user_id'])
print(req2.cookies['sessionid'])

If you use requests.Session() , you won't need the cookie header.如果您使用requests.Session() ,则不需要cookie标头。

Making a few changes to your solution;对您的解决方案进行一些更改; you can use this:你可以使用这个:

import requests

url = 'https://www.instagram.com/accounts/login/'
url_main = url + 'ajax/'
auth = {'username': 'login', 'password': 'pass'}
headers = {'referer': "https://www.instagram.com/accounts/login/"}

with requests.Session() as s:
    req = s.get(url)
    headers['x-csrftoken'] = req.cookies['csrftoken']
    s.post(url_main, data=auth, headers=headers)
    # Now, you can use 's' object to 'get' response from any instagram url
    # as a logged in user.
    r = s.get('https://www.instagram.com/accounts/edit/')
    # If you want to check whether you're getting the correct response,
    # you can use this:
    print(auth['username'] in r.text)  # which returns 'True'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM