Skip to content

Instantly share code, notes, and snippets.

@Sigafoos
Created September 18, 2017 17:31
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 Sigafoos/5bdfeb8ef53bb44634f06223dd19dba7 to your computer and use it in GitHub Desktop.
Save Sigafoos/5bdfeb8ef53bb44634f06223dd19dba7 to your computer and use it in GitHub Desktop.
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