pythonpostrequestpython-requestsinstagram

Python change instagram profile-picture with requests


I'm trying to change my Instagram profile picture with a python post request, but it does not change. I don't get any errors and in the response, it says that they changed it, but to a white Instagram person picture and not my chosen picture.

request url: https://www.instagram.com/accounts/web_change_profile_picture/

request headers: :authority: www.instagram.com
:method: POST
:path: /accounts/web_change_profile_picture/
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7
content-length: 251409
content-type: multipart/form-data; boundary=----WebKitFormBoundarySbMgVbqkrGZYUfnE
cookie: ig_cb=1; mid={mid}; mcd=3; rur=ATN; ig_gdpr_signup=1; csrftoken={my_csrf}; shbid=9320; shbts=1543326033.1119306; ds_user_id={my_userid}; sessionid=3215216108%3A3qIcf1SYxlvIOd%3A29; urlgen="{my_urlgen}:7zHjeu:bVP01Os-2jGH6RETg-jCpkDsaRf"
origin: https://www.instagram.com
referer: https://www.instagram.com/{username}/
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36
x-csrftoken: {my_csrf}
x-instagram-ajax: {my_ajax}
x-requested-with: XMLHttpRequest

Form data: 
profile_pic: (binary)

View Source form data:
------WebKitFormBoundarySbMgVbqkrGZYUfnE
Content-Disposition: form-data; name="profile_pic"; filename="profilepic.jpg"
Content-Type: image/png


------WebKitFormBoundarySbMgVbqkrGZYUfnE--

My code:
s.headers.update(headers_above)
img_payload = {
              "file": ("blo.jpg", open("D:\\{path_to_jpg_file}", "rb"), "image/jpeg")
                    }
img_url = "https://www.instagram.com/accounts/web_change_profile_picture/"
 r3 = s.post(img_url, files=img_payload)

Solution

    1. After sniffing the upload process via firefox developer tools, I copied all the request headers to the headersof post request, but it didn't work, then I removed Content-Type": "multipart/form-data; boundary=-...XXXXX" and boom!
    2. I also updated the "Content-Length" value with os.path.getsize("my_new_pp.jpg")
    3. Make sure you refresh the profile pic page with no cache, on FF, hold down the Shift key and left-click the Reload button.

    I managed to change my profile pic on instagram using the following code:

    import os
    import requests
    
    p_pic = "my_new_pp.jpg"
    p_pic_s = os.path.getsize("my_new_pp.jpg")
    
    headers = {
    "Host": "www.instagram.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36",
    "Accept": "*/*",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate, br",
    "Referer": "https://www.instagram.com/pedro_lobito/",
    "X-CSRFToken": "YOUR_X-CSRFToken",
    "X-Instagram-AJAX": "YOUR_X-Instagram-AJAX",
    "X-Requested-With": "XMLHttpRequest",
    "Content-Length": str(p_pic_s), # should also work without this
    "DNT": "1",
    "Connection": "keep-alive",
    "Cookie": "YOUR_COOKIE"
    }
    
    url = "https://www.instagram.com/accounts/web_change_profile_picture/"
    
    files = {'profile_pic': open(p_pic,'rb')}
    values = {"Content-Disposition": "form-data", "name": "profile_pic", "filename":"profilepic.jpg",
    "Content-Type": "image/jpeg"}
    
    r = requests.post(url, files=files, data=values, headers=headers)
    print(r.text)
    

    Response:

    {"changed_profile": true, "id": 233885295, "has_profile_pic": true, "profile_pic_url": "https://instagram.flis9-1.fna.fbcdn.net/vp/09bb6c124303a825a67da0cb092c9ee7/5C8F561F/t51.2885-19/s150x150/44814766_1606677162811158_2721039563597283328_n.jpg", "profile_pic_url_hd": "https://instagram.flis9-1.fna.fbcdn.net/vp/adb01e186d733d3660c300db5bae41a9/5C79DA67/t51.2885-19/s320x320/44814766_1606677162811158_2721039563597283328_n.jpg", "status": "ok"}