0

So I have this Python Script

import base64
import hmac
import hashlib
import time
import random
import urllib.parse
import requests
from requests_oauthlib import OAuth1

# Replace these values with your own keys and tokens
consumer_key = "VVVVVVVVVVVVV"
consumer_secret = "XXXXXXXXXXXXXXX"
access_token = "YYYYYYYYYYYYYYYY"
access_token_secret = "ZZZZZZZZZZZZZZZZZZZZz"

# OAuth1 authentication
auth = OAuth1(consumer_key, consumer_secret, access_token, access_token_secret)

# API endpoints
tweet_endpoint = "https://api.twitter.com/1.1/statuses/update.json"
media_upload_endpoint = "https://upload.twitter.com/1.1/media/upload.json"

# Upload the image
image_path = "/home/ep/data/MyPythonPrograms/agent-director/storytweets/first_api_tweet.png"

with open(image_path, "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode("utf-8")

upload_response = requests.post(media_upload_endpoint, data={"media_data": image_data}, auth=auth)
upload_response.raise_for_status()
media_id = upload_response.json()["media_id_string"]

# Send the tweet with the image
tweet_text = "Coming soon - Twitter API test 3"
tweet_response = requests.post(tweet_endpoint, data={"status": tweet_text, "media_ids": media_id}, auth=auth)
tweet_response.raise_for_status()
tweet_data = tweet_response.json()
print(tweet_data)

print(f"Tweet posted: https://twitter.com/{tweet_data['user']['screen_name']}/status/{tweet_data['id_str']}")

However even though I can see the APP is authorized to access profile, the keys all match up I am not seeng anything being posted to the twitter profile timeline.

Even get a someone tried to log in if it was you disregard this message - So I believe the script is accessing like it is supposed to.

Any good ideas as to why I don't see anything on the timeline, have both v1 and v2 access to the API

  • some general ideas: 1) check API permissions 2) check the error response print(upload_response.content) print(tweet_response.content) – Mazur Ekaterina May 15 '23 at 19:16
  • Cheers, tried getting error msg's but did'nt get closer, didn't get anything like I said, I got a ping on having logged in using the app inside my regular twitter, i.e the authentication was working.... Jury rigged a posting system in Python that can post as long as I am logged in via the browser. Will be looking further into this when time allows. – Einar Petersen May 22 '23 at 23:51

0 Answers0