Account activity registering webhook program freeze up

account-activity

#1

I am using app authentication and get a bear token. But then when I post to register a web hook the program just freezes. It is not trying to communicate with the url I want to register as there is no activity in that log. Here is the relevant parts of my code and the debug log. The token is a valid bearer token.

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.twitter.com

headers = {
“Host”: “api.twitter.com”,
“User-Agent”: “My Twitter App v1.0.23”,
“Authorization”: "Bearer " + token,
“Content-Type”: “application/x-www-form-urlencoded”,
“Content-Length”: “29”,
“Accept-Encoding”: “/
}

data = {
“url” : callbackURL
}

r = requests.post(“https://api.twitter.com/1.1/account_activity/all/env-beta/webhooks.json”, headers=headers, params=data)

print (r.text)


#2

I solved it. You need to use oauth1 (i.e. user) authentication and not app authentication to get a bearer token. Here is a complete program to help similarly confused persons:

import urllib.parse
import base64
import logging
import requests
import simplejson as json
from requests_oauthlib import OAuth1

#logging.basicConfig(level=logging.DEBUG)

consumerKey = "XXXXX"
consumerSecret ="XXXXX"
accessToken ="XXXXX"
accessTokenSecret =“XXXXX”

url = ‘https://api.twitter.com/oauth/request_token

auth = OAuth1(consumerKey, consumerSecret, accessToken, accessTokenSecret)
r = requests.get(url, auth=auth)

authToken=r.text.split("=")[1].split("&")[0]
print (“oauth_token=”, authToken)

callbackURL = urllib.parse.urlencode({‘url’: “http://foo.com”})

print (“callbackURL=”, callbackURL)

data = {
“url” : callbackURL
}

r = requests.post(“https://api.twitter.com/1.1/account_activity/all/env-beta/webhooks.json”, auth=auth, params=data)

print (r.text)