Reputation: 1
`# Create Client object with OAuth 2.0
client = tweepy.Client(
consumer_key=self.twitter_consumer_key,
consumer_secret=self.twitter_consumer_secret,
access_token=self.twitter_access_token,
access_token_secret=self.twitter_access_token_secret
)
# Create API v1.1 object for media upload
auth = tweepy.OAuth1UserHandler(
self.twitter_consumer_key,
self.twitter_consumer_secret,
self.twitter_access_token,
self.twitter_access_token_secret
)
api = tweepy.API(auth)
try:
# Simple media upload without chunked option
print("Attempting media upload...")
with open(image_path, 'rb') as image_file:
media = api.simple_upload(filename=image_path, file=image_file)
print(f"Media uploaded successfully. Media ID:{media.media_id}")`
I am using a v1 client object for uploading media, and a v2 client object for creating a tweet. However, the program stops at uploading media with the following two errors:
The first time I run it gives me an 403 Forbidden error
.
The second time I run it gives me <class 'simplejson.errors.JSONDecodeError'>
type error with the error string as Expecting value: line 1 column 1 (char 0)
. I checked for a response object but seems like it doesn't return any response object. I have tried both api functions: media_upload
and simple_upload
and I get the same error.
Function call for medi_upload: media = api.media_upload( filename=image_path, chunked=True )
Here are the configurations of my app project on my Twitter dev account
App permissions, type of app App info
Does anyone know how to solve this? Thanks in advance!
Expecting the media file to be successfully uploaded. Tried different media files, checked if the file is present and opens properly before the function call. The file opens and prints the file size ( around 1-2 MBs ) before the media_upload
/ simple_upload
function calls, but fails when the API makes the media upload call. Tried downgrading and upgrading the tweepy package to 4.14.0, 4.9.0, and 4.12.0. I still get the same results across all the package versions.
Edit: Adding tracebacks for both errors:
Traceback (most recent call last):
File "/home/user/test.py", line 540, in <module>
main()
File "/home/user/test.py", line 532, in main
publisher.post_to_twitter(temp_path, description)
File "/home/user/test.py", line 391, in post_to_twitter
media = self.twitter_v1.media_upload(image_path)
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 3566, in media_upload
return self.simple_upload(
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 3610, in simple_upload
return self.request(
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 265, in request
raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
Traceback (most recent call last):
File "/home/user/test.py", line 540, in <module>
main()
File "/home/user/test.py", line 532, in main
publisher.post_to_twitter(temp_path, description)
File "/home/user/test.py", line 391, in post_to_twitter
media = self.twitter_v1.media_upload(image_path)
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 3566, in media_upload
return self.simple_upload(
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 3610, in simple_upload
return self.request(
File "/home/user/.local/lib/python3.10/site-packages/tweepy/api.py", line 265, in request
raise Forbidden(resp)
File "/home/user/.local/lib/python3.10/site-packages/tweepy/errors.py", line 54, in __init__
response_json = response.json()
File "/home/user/.local/lib/python3.10/site-packages/requests/models.py", line 974, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/local/lib/python3.10/site-packages/simplejson/__init__.py", line 525, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.10/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/local/lib/python3.10/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Upvotes: 0
Views: 26