Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Created April 20, 2016 17:45
Show Gist options
  • Save jcipriano/c58148286df486b1dcb75e4e99caef68 to your computer and use it in GitHub Desktop.
Save jcipriano/c58148286df486b1dcb75e4e99caef68 to your computer and use it in GitHub Desktop.
Twitter Enagement API Request Example
import requests
import json
from requests_oauthlib import OAuth1
API_URL = 'https://data-api.twitter.com/insights/engagement'
CONSUMER_KEY = 'YOUR_CONSUMER_KEY'
CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET'
ACCESS_TOKEN = 'USER_ACCESS_TOKEN'
ACCESS_TOKEN_SECRET = 'USER_ACCESS_TOKEN_SECRET'
def get_oauth():
oauth = OAuth1(CONSUMER_KEY,
client_secret=CONSUMER_SECRET,
resource_owner_key=ACCESS_TOKEN,
resource_owner_secret=ACCESS_TOKEN_SECRET)
return oauth
if __name__ == '__main__':
params = {
'tweets': {
'tweet.id': [
'A-TWEET-ID'
]
},
'engagements': {
'start': '2015-04-29',
'end': '2015-05-11',
'types': [
'impressions',
'url_clicks'
]
},
'groupings': {
'engagements_by_type': {
'group_by': [
'engagement.type'
]
}
}
}
headers = { 'content-type': 'application/json' }
oauth = get_oauth()
req = requests.post(url=API_URL, data=json.dumps(params), headers=headers, auth=oauth)
print req.text
@jcipriano
Copy link
Author

pip install requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment