简体   繁体   English

如何使用帖子请求将照片上传到 Instagram

[英]How upload photo to instagram with post requests python

I'm trying to compose a post request for uploading a photo to Instagram, but the third request returns a 403 error to "instagram.com/create/configure/"我正在尝试撰写将照片上传到 Instagram 的帖子请求,但第三个请求向“instagram.com/create/configure/”返回 403 错误

My code:我的代码:

import re
import requests
import urllib.request
from datetime import datetime

link = 'https://www.instagram.com/accounts/login/'
login_url = 'https://www.instagram.com/accounts/login/ajax/'

login = 'login_name'
password = '0000'

time = int(datetime.now().timestamp())
print(time)
payload = {
    'username': login,
    'enc_password': f'#PWD_INSTAGRAM_BROWSER:0:{time}:{password}',
    'queryParams': {},
    'optIntoOneTap': 'false'
}

with requests.Session() as s:
    r = s.get(link)
    a = r.cookies['csrftoken']
    print(r.cookies)
    print(a)
    csrf = re.findall(r"csrf_token\":\"(.*?)\"", r.text)[0]
    print(csrf)

    r = s.post(login_url, data=payload, headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "Referer": "https://www.instagram.com/accounts/login/",
        "x-csrftoken": csrf
    })
    print(r.status_code)
    print(r.url)
    print(r.text)
    print(s.cookies)
    r = s.get('https://www.instagram.com/accounts/edit/')
    print(login in r.text)


    microtime = int(datetime.now().timestamp())
    headers =  {
        "content-type": "image / jpg",
        "X-Entity-Name" : f"fb_uploader_{microtime}",
        "Offset": "0",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "x-entity-length": "299255",
        "X-Instagram-Rupload-Params": f'{{"media_type": 1, "upload_id": {microtime}, "upload_media_height": 1080, "upload_media_width": 1080}}',
        "x-csrftoken": csrf,
        "x-ig-app-id": "1217981644879628"
    }

    img = urllib.request.urlopen('https://sun9-64.userapi.com/RVgUHSq9fXrDr8YBJ4a4h9xwN4EQA_8BXuQ5Vg/Mdx3LwawEmY.jpg')
    photo = img.read()
    r = s.post(f'https://www.instagram.com/rupload_igphoto/fb_uploader_{microtime}', data=open("4.jpg", "rb"), headers=headers)
    print(r.text)
    headers = {
        'Content-Length': '104',
        'content-type': 'application/x-www-form-urlencoded',
        "origin": "https://www.instagram.com",
        "referer": "https://www.instagram.com/create/details/",
        'user-agent': "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "x-csrftoken": csrf,
        "x-ig-app-id": "1217981644879628",
        "X-Requested-With": "XMLHttpRequest"
    }

    body = {
        'upload_id': f'{microtime}',
        "caption":'text for caption',
        'usertags':'',
        'custom_accessibility_caption': '',
        'retry_timeout':''
    }
    r = s.post('https://www.instagram.com/create/configure/', data=body, headers=headers)
    print(r.status_code)
    print(r.text)

Process: First authorization, everything is okay with that流程:首次授权,一切正常

The second request is to upload a photo, everything is ok too第二个请求是上传照片,一切都还可以

The third request, with the parameters of the photo, there is an error ... error 403 is returned第三次请求,带着照片的参数,有错误……返回错误403

尝试使用智能手机用户代理并在上传响应中检索 user_id,然后将其设置在帖子正文中而不是时间中,您在这方面帮了我很多:)

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

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