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)