设置instagram帐户私人python请求

时间:2019-07-10 15:40:04

标签: python python-3.x python-requests instagram

由于某种原因,我无法通过一个简单的http请求将我的帐户设置为私人帐户。我可以很好地编辑配置文件,但是当我发送请求将我的帐户设置为私有时,它会给我200状态代码以及成功的响应消息,但是当检查我的配置文件时,该帐户不是私有的... < / p>

这是我的代码:

import requests
import json

class IGWebAPI:

    def __init__(self, username, password, user_agent):
        self.session = requests.Session()
        self.username = username
        self.password = password
        self.user_agent = user_agent
        self.login()

    def login(self):
        self.session.headers = {'user-agent': self.user_agent}
        self.session.get('https://www.instagram.com/accounts/login/ajax/')
        self.session.headers.update({'Referer': 'https://www.instagram.com/accounts/login/', 'x-csrftoken': self.session.cookies['csrftoken']})
        self.session.post('https://www.instagram.com/accounts/login/ajax/', data={'username': self.username, 'password': self.password, 'queryParams' : r'{}'}, allow_redirects=True)
        self.session.headers.update({'x-csrftoken': self.session.cookies['csrftoken']})

    def make_profile_private(self):
        self.session.get('https://www.instagram.com/accounts/privacy_and_security/')
        self.session.headers.update(
            {'Referer' :'https://www.instagram.com/accounts/privacy_and_security/'})
        a = self.session.post(
                'https://www.instagram.com/accounts/set_private/',
                data = {
                    'is_private' : True
                }
            )
        print(a.text)

username = ''
password = ''
user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
api = IGWebAPI(username, password, user_agent)
api.make_profile_private()

编辑:必须添加一行以使用csrf令牌更新标头。

0 个答案:

没有答案