Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
In CircuitPython
import json
import urequests
import base64
with open("config.json") as fp:
config = json.load(fp)
if config['key'] is None:
raise KeyError("No twitter API key")
if config['secret'] is None:
raise KeyError("No twitter API secret")
# you should be url encoding the keys but we can't in CP
key = base64.b64encode(config['key'] + ':' + config['secret'])
url = "https://api.twitter.com/oauth2/token"
header = {
"Authorization": "Basic " + key,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
body = {
"grant_type": "client_credentials"
}
r = urequests.post(url, data=body, headers=header)
decoded = r.json()
token = decoded['access_token']
username = "sigafoos"
url = "https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=%s&count=5000" % username
header = {
"Authorization": "Bearer " + token
}
r = urequests.get(url, headers=header)
decoded = r.json()
num_followers = len(decoded['ids'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.