Skip to content

Instantly share code, notes, and snippets.

@sleepless-se
Last active March 30, 2024 23:35
Show Gist options
  • Save sleepless-se/8784328322311a5e86cfa0e2ae91addd to your computer and use it in GitHub Desktop.
Save sleepless-se/8784328322311a5e86cfa0e2ae91addd to your computer and use it in GitHub Desktop.
This script for create twitter ACCESS TOKEN KEY and ACCESS TOKEN SECRET.
import tweepy
import webbrowser
import urllib
CONSUMER_KEY = "*****"
CONSUMER_SECRET = "*****"
def get_oauth_token(url:str)->str:
querys = urllib.parse.urlparse(url).query
querys_dict = urllib.parse.parse_qs(querys)
return querys_dict["oauth_token"][0]
if __name__ == '__main__':
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
try:
redirect_url = auth.get_authorization_url()
print ("Redirect URL:",redirect_url)
except tweepy.TweepError:
print( "Error! Failed to get request token.")
oauth_token = get_oauth_token(redirect_url)
print("oauth_token:", oauth_token)
auth.request_token['oauth_token'] = oauth_token
# Please confirm at twitter after login.
webbrowser.open(redirect_url)
verifier = input("You can check Verifier on url parameter. Please input Verifier:")
auth.request_token['oauth_token_secret'] = verifier
try:
auth.get_access_token(verifier)
except tweepy.TweepError:
print('Error! Failed to get access token.')
print("access token key:",auth.access_token)
print("access token secret:",auth.access_token_secret)
with open("auth_info.text",mode="w") as file:
text = "key:{}\nsecret:{}".format(auth.access_token,auth.access_token_secret)
file.write(text)
print("DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment