Created
September 18, 2017 17:31
-
-
Save Sigafoos/5bdfeb8ef53bb44634f06223dd19dba7 to your computer and use it in GitHub Desktop.
In CircuitPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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