将二进制图像上传到instagram发布请求

时间:2018-11-23 22:25:47

标签: python python-3.x https http-headers python-requests

我正在尝试通过HTTP请求以编程方式更改instagram上的用户个人资料图片。我不确定为什么个人资料图片没有更新。我已经使用了许多关于stackoverflow(Upload Image using POST form data in Python-requests)的示例以及用于请求的官方文档(http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file)。这是我目前正在使用的方法:

def update_profile_pic(self):
        get_change_pic = self.session.get('https://www.instagram.com/' + self.username, headers=self.headers)

        self.session.headers.update({
            'user-agent' : self.user_agent,
            'x-csrftoken' : get_change_pic.cookies['csrftoken'],
            'content-type' : 'multipart/form-data; boundary=----WebKitFormBoundaryUIboB46mvoIt1m9R',
            'referer' : 'https://www.instagram.com/' + self.username + '/',
            'origin' : 'https://www.instagram.com'
        })

        files = {'profile_pic': ('profilepic.jpg', 
        open('profilepic.jpg', 'rb').read(), 
        'image/jpeg', 
        {
            'Content-Disposition' : 'form-data;',
        }
        )}

        change_pic = self.session.post('https://www.instagram.com/accounts/web_change_profile_picture/', files=files)

        print(change_pic.text)

这是我从程序中得到的响应:

{"changed_profile": true, "id": 9129479436, "has_profile_pic": false, "profile_pic_url": "https://instagram.fmkc1-1.fna.fbcdn.net/vp/c012aa3473e225c235b274cb8a51c620/5C716EDF/t51.2885-19/38905493_2193354514245732_6791661364604567552_n.jpg", "profile_pic_url_hd": "https://instagram.fmkc1-1.fna.fbcdn.net/vp/c012aa3473e225c235b274cb8a51c620/5C716EDF/t51.2885-19/38905493_2193354514245732_6791661364604567552_n.jpg", "profile_pic_to_post_upsell_eligible": true, "status": "ok"}

这是我通过instagram.com手动上传图片时得到的回复:

{"changed_profile": true, "id": 9129479436, "has_profile_pic": true, "profile_pic_url": "https://instagram.fbed1-1.fna.fbcdn.net/vp/827163eecb3fb3ed2cd7969528fd92da/5C6A10A3/t51.2885-19/44785389_226951028028925_6866362025681879040_n.jpg", "profile_pic_url_hd": "https://instagram.fbed1-1.fna.fbcdn.net/vp/827163eecb3fb3ed2cd7969528fd92da/5C6A10A3/t51.2885-19/44785389_226951028028925_6866362025681879040_n.jpg", "status": "ok"}

响应头:

access-control-allow-credentials: true
access-control-allow-origin: https://www.instagram.com
cache-control: private, no-cache, no-store, must-revalidate
content-encoding: gzip
content-language: en
content-length: 217
content-security-policy: report-uri https://www.instagram.com/security/csp_report/; default-src 'self' https://www.instagram.com; img-src https: data: blob:; font-src https: data:; media-src 'self' blob: https://www.instagram.com https://*.cdninstagram.com https://*.fbcdn.net; manifest-src 'self' https://www.instagram.com; script-src 'self' https://instagram.com https://www.instagram.com https://*.www.instagram.com https://*.cdninstagram.com wss://www.instagram.com https://*.facebook.com https://*.fbcdn.net https://*.facebook.net 'unsafe-inline' 'unsafe-eval' blob:; style-src 'self' https://*.www.instagram.com https://www.instagram.com 'unsafe-inline'; connect-src 'self' https://instagram.com https://www.instagram.com https://*.www.instagram.com https://graph.instagram.com https://*.graph.instagram.com https://*.cdninstagram.com https://api.instagram.com wss://www.instagram.com wss://edge-chat.instagram.com https://*.facebook.com https://*.fbcdn.net https://*.facebook.net chrome-extension://boadgeojelhgndaghljhdicfkmllpafd; worker-src 'self' https://www.instagram.com; frame-src 'self' https://instagram.com https://www.instagram.com https://staticxx.facebook.com https://www.facebook.com https://web.facebook.com https://connect.facebook.net https://m.facebook.com; object-src 'none'; upgrade-insecure-requests
content-type: application/json; charset=utf-8
date: Sun, 18 Nov 2018 03:22:59 GMT
expires: Sat, 01 Jan 2000 00:00:00 GMT
pragma: no-cache
set-cookie: rur=ATN; Domain=.instagram.com; HttpOnly; Path=/; Secure
set-cookie: ds_user_id=9129479436; Domain=.instagram.com; expires=Sat, 16-Feb-2019 03:22:59 GMT; Max-Age=7776000; Path=/; Secure
set-cookie: urlgen="{}:1gODfn:oseVSKSqi_G7dsrw-sGyYnB4UB8"; Domain=.instagram.com; HttpOnly; Path=/; Secure
set-cookie: csrftoken=CQM7TvwBhKbigdcj2a7UVZNaAXB0xGpu; Domain=.instagram.com; expires=Sun, 17-Nov-2019 03:22:59 GMT; Max-Age=31449600; Path=/; Secure
status: 200
strict-transport-security: max-age=60
vary: Accept-Language, Cookie, Accept-Encoding
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 0

请求标头:

:authority: www.instagram.com
:method: POST
:path: /accounts/web_change_profile_picture/
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
content-length: 8638
content-type: multipart/form-data; boundary=----WebKitFormBoundaryf3YOtJCAfKhjZCVf
cookie: mid=W_DaPQAEAAGEpui-cKxkbCScmijn; mcd=3; csrftoken=CQM7TvwBhKbigdcj2a7UVZNaAXB0xGpu; ds_user_id=9129479436; sessionid=IGSC24a182fcba699695123d686dee80a45615c1368c599d73392433f7a465714a70%3A8Bc4cb2j59ZJJFGKCLXEa4v3uUJBz63g%3A%7B%22_auth_user_id%22%3A9129479436%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_platform%22%3A4%2C%22_token_ver%22%3A2%2C%22_token%22%3A%229129479436%3AUgJfZH2OoUcdCLyhaMthiwJgIXATamnB%3A2ed3ce04127d527af4a4187d2c9944612604de61d70a93abbc3f5c6ed98cb6be%22%2C%22last_refreshed%22%3A1542511180.8014497757%7D; rur=ATN; urlgen="{}:1gODfZ:3h97w_2Cpg3f0VTSrZDw5pyC7fY"
origin: https://www.instagram.com
referer: https://www.instagram.com/heeyioep/
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
x-csrftoken: CQM7TvwBhKbigdcj2a7UVZNaAXB0xGpu
x-instagram-ajax: 710a1be42428
x-requested-with: XMLHttpRequest

表单数据(已解析):

profile-pic: (binary)

表单数据(源):

------WebKitFormBoundaryf3YOtJCAfKhjZCVf
Content-Disposition: form-data; name="profile_pic"; filename="profilepic.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryf3YOtJCAfKhjZCVf--

我相信我已经设置了所有适当的标题。发送请求时收到响应,但没有更改个人资料图片。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以使用 Instagram API 上传图片/视频。

安装 InstagramApi

pip install InstagramApi

测试登录

from InstagramAPI import InstagramAPI

api = InstagramAPI("username", "password")
if (api.login()):
    api.getSelfUserFeed()  # get self user feed
    print(api.LastJson)  # print last response JSON
    print("Login succes!")
else:
print("Can't login!")

上传带有标题的图像

from InstagramAPI import InstagramAPI

InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login()  # login

photo_path = '/path/to/photo.jpg'
caption = "Sample photo"
InstagramAPI.uploadPhoto(photo_path, caption=caption)

上传视频:

from InstagramAPI import InstagramAPI
import urllib

video_url = 'https://instagram.fmad3-2.fna.fbcdn.net/t50.2886-16/17157217_1660580944235536_866261046376005632_n.mp4'  # a valid instagram video
video_local_path = video_url.split("/")[-1]
thumbnail_url = "https://instagram.fmad3-2.fna.fbcdn.net/t51.2885-15/e15/17075853_1759410394387536_3927726791665385472_n.jpg"
thumbnail_local_path = thumbnail_url.split("/")[-1]

urllib.urlretrieve(video_url, video_local_path)
urllib.urlretrieve(thumbnail_url, thumbnail_local_path)

InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login()  # login
InstagramAPI.uploadVideo(video_local_path, thumbnail_local_path, caption="Tortuguero")

答案 1 :(得分:0)

我尝试了以下方法:

from InstagramAPI import InstagramAPI

InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login()  # login

photo_path = '/path/to/photo.jpg'
caption = "Sample photo"
InstagramAPI.uploadPhoto(photo_path, caption=caption)

但是它在python控制台上给了我 False