Successful Completed Task of Tweet using Python

Successful Completed Task of Tweet using Python


go here https://developer.x.com/en/portal/projects create your consumer api key and consumer secret key

got to project & apps then click on id then click on user authentication settings edit button



Then give read and write permission


here is code put your consumer_key and consumer_secreate key

from requests_oauthlib import OAuth1Session
import os
import json

consumer_key = "Your consumer key"
consumer_secret = "Your scecret key"

payload = {"text": "Hello world!"}

# Get request token
request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret)

try:
    fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
    print(
        "There may have been an issue with the consumer_key or consumer_secret you entered."
    )

resource_owner_key = fetch_response.get("oauth_token")
resource_owner_secret = fetch_response.get("oauth_token_secret")
print("Got OAuth token: %s" % resource_owner_key)

# Get authorization
base_authorization_url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(base_authorization_url)
print("Please go here and authorize: %s" % authorization_url)
verifier = input("Paste the PIN here: ")

# Get the access token
access_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=resource_owner_key,
    resource_owner_secret=resource_owner_secret,
    verifier=verifier,
)
oauth_tokens = oauth.fetch_access_token(access_token_url)

access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]

# Make the request
oauth = OAuth1Session(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=access_token,
    resource_owner_secret=access_token_secret,
)

# Making the request
response = oauth.post(
    "https://api.twitter.com/2/tweets",
    json=payload,
)

if response.status_code != 201:
    raise Exception(
        "Request returned an error: {} {}".format(response.status_code, response.text)
    )

print("Response code: {}".format(response.status_code))

# Saving the response as JSON
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))        

after this copy that link https://api.twitter.com/oauth/authorize?oauth_token=sGYNZQAAAAABuqw0AAABkjNTWN8 and paste in your chrome browser


then it will show like this


click on authorize app then you will be code


copy this code and Paste the pin in your runtime

(Note: That pin is different in above image so dont worry just paste as it is in your process exact same pin! )


it will run and tweet Hello World


So this was the process to tweet this

src to learn : https://github.com/xdevplatform/Twitter-API-v2-sample-code/blob/main/Manage-Tweets/create_tweet.py


IF You Have Some Confusion Just ping me or mail me faizanshaikh0001@gmail.com

I Love To Help You.

Thanks You :)

要查看或添加评论,请登录

Faizan Shaikh的更多文章

社区洞察

其他会员也浏览了