Home > Enterprise >  Why twitter api request get Expecting value: line 1 column 1 (char 0)?
Why twitter api request get Expecting value: line 1 column 1 (char 0)?

Time:01-05

I got the below error when using a Twitter API request to change PFP:

'Expecting value: line 1 column 1 (char 0)'

And this for the Code

picture = [f for f in os.listdir("pfp/") if isfile(join("pfp/", f))]
random_picture = random.choice(picture)

with open(f'pfp/{random_picture}', "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())

cookies = {
            'auth_token': x
        }
headers = {'Content-Type': 'multipart/form-data'}
params = f"image=data:image/png;base64,{(encoded_string.decode('utf-8'))}"

response = requests.post('https://api.twitter.com/1.1/account/update_profile_image.json', 
cookies=cookies, params=params, headers=headers, proxies=proxies)

is the my code wrong ?

Any help is appreciated.

CodePudding user response:

Thing which you need to provide:

  • method: POST

  • url: https://api.twitter.com/1.1/account/update_profile_image.json

  • parameter as encoded url-safe Base64 (so for example should be changed to ) for example: iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAACXBIWXMAABwgAAAcIAHND5ueAAAAJklEQVQImWMQibsx73 NSNyNGyIiDBAWhGSAs b9r2G4ISIC5wMAg IY55xszl4AAAAASUVORK5CYII=

  • header to authorise user: auth_token: 1610678221XXX-ab5sYYYYY

  • headers to authorise application (for OAuth 1.0 HMAC-SHA1)

Authorization headers - RFC 5849: The OAuth 1.0 Protocol

How to build it, check here:

https://www.rfc-editor.org/rfc/rfc5849#section-3.5.1

or here:

https://stackoverflow.com/a/67668940/5529263

For python you can use Requests-OAuthlib library

Example of code:

import requests

image = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAACXBIWXMAABwgAAAcIAHND5ueAAAAJklEQVQImWMQibsx73 NSNyNGyIiDBAWhGSAs b9r2G4ISIC5wMAg IY55xszl4AAAAASUVORK5CYII="

url = "https://api.twitter.com/1.1/account/update_profile_image.json?image="   image

headers = {
    'auth_token': '12341234512345234512345-abcedfghijklm',
    'Authorization': 'OAuth '
                     'oauth_consumer_key="ABCD1234ABCD1234ABCD1234",'
                     'oauth_signature_method="HMAC-SHA1",'
                     'oauth_timestamp="1234567890",'
                     'oauth_nonce="MNOPRST0987MN",'
                     'oauth_version="1.0",'
                     'oauth_signature="ABCDEFGHIJ1234ABCDEFGHIJ"',
}

response = requests.request("POST", url, headers=headers, data={})

print(response.text)

After sending correct request you can receive response with code 403

{
    "errors": [
        {
            "message": "You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve",
            "code": 453
        }
    ]
}

so it means you need to apply for Elevated access, here you have more information: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-level

  • Related