Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active September 2, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edsu/1e2c4fc2ea2dae90ea8fb660b708e690 to your computer and use it in GitHub Desktop.
Save edsu/1e2c4fc2ea2dae90ea8fb660b708e690 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# For this to work you need to install requests_oauthlib and put your keys in
# the environment: CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, and
# ACCESS_TOKEN_SECRET. Also you will need to go into the new Twitter dashboard
# and put the identified app in a Project in order for it to use the new v2 API.
import os
import json
import requests
from requests_oauthlib import OAuth1, OAuth1Session, OAuth2Session
url = 'https://api.twitter.com/2/tweets'
consumer_key = os.environ.get('CONSUMER_KEY')
consumer_secret = os.environ.get('CONSUMER_SECRET')
access_token = os.environ('ACCESS_TOKEN')
access_token_secret = os.environ('ACCESS_TOKEN_SECRET')
http = OAuth1Session(
client_key=consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret
)
params = {
"ids": "1297495295266357248",
"expansions": "attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id",
"media.fields": "duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics",
"place.fields": "contained_within,country,country_code,full_name,geo,id,name,place_type",
"poll.fields": "duration_minutes,end_datetime,id,options,voting_status",
"tweet.fields":"attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,source,text,withheld",
"user.fields": "created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld"
}
resp = http.get(url, params=params)
print(json.dumps(resp.json(), indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment