Advertisement
zeromega64twenty

2023 x.com auto post auto tweet

Aug 30th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | Software | 0 0
  1. #This is not best practice but it does work and will post to x.com (the only test I have run so far is a simple hello world). Run it with the sample message to let me know you found this.
  2.  
  3. from requests_oauthlib import OAuth1Session
  4. import os
  5. import json
  6.  
  7. # Set your environment variables here
  8. os.environ["CONSUMER_KEY"] = "PASTE YOUR KEY HERE"
  9. os.environ["CONSUMER_SECRET"] = "PASTE YOUR KEY HERE"
  10.  
  11. # Retrieve environment variables
  12. consumer_key = os.environ.get("CONSUMER_KEY") #Do not adjust this
  13. consumer_secret = os.environ.get("CONSUMER_SECRET") #Do not adjust this
  14.  
  15. # Be sure to replace the text with the content you want to Tweet.
  16. payload = {"text": "Hello world! @zeromega64"}
  17.  
  18. # Get request token
  19. request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"
  20. oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)
  21.  
  22. try:
  23.     fetch_response = oauth.fetch_request_token(request_token_url)
  24. except ValueError:
  25.     print(
  26.         "There may have been an issue with the consumer_key or consumer_secret you entered."
  27.     )
  28.     exit()
  29.  
  30. resource_owner_key = fetch_response.get("oauth_token")
  31. resource_owner_secret = fetch_response.get("oauth_token_secret")
  32. print("Got OAuth token: %s" % resource_owner_key)
  33.  
  34. # Get authorization
  35. base_authorization_url = "https://api.twitter.com/oauth/authorize"
  36. authorization_url = oauth.authorization_url(base_authorization_url)
  37. print("Please go here and authorize: %s" % authorization_url)
  38. verifier = input("Paste the PIN here: ")
  39.  
  40. # Get the access token
  41. access_token_url = "https://api.twitter.com/oauth/access_token"
  42. oauth = OAuth1Session(
  43.     consumer_key,
  44.     client_secret=consumer_secret,
  45.     resource_owner_key=resource_owner_key,
  46.     resource_owner_secret=resource_owner_secret,
  47.     verifier=verifier,
  48. )
  49. oauth_tokens = oauth.fetch_access_token(access_token_url)
  50.  
  51. access_token = oauth_tokens["oauth_token"]
  52. access_token_secret = oauth_tokens["oauth_token_secret"]
  53.  
  54. # Make the request
  55. oauth = OAuth1Session(
  56.     consumer_key,
  57.     client_secret=consumer_secret,
  58.     resource_owner_key=access_token,
  59.     resource_owner_secret=access_token_secret,
  60. )
  61.  
  62. # Making the request
  63. response = oauth.post(
  64.     "https://api.twitter.com/2/tweets",
  65.     json=payload,
  66. )
  67.  
  68. if response.status_code != 201:
  69.     raise Exception(
  70.         "Request returned an error: {} {}".format(response.status_code, response.text)
  71.     )
  72.  
  73. print("Response code: {}".format(response.status_code))
  74.  
  75. # Saving the response as JSON
  76. json_response = response.json()
  77. print(json.dumps(json_response, indent=4, sort_keys=True))
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement