def authorize(filepath=None):
"""
Create an authorization object for use with the requests
library. Takes the path to a yaml-encoded file containing twitter API keys.
:param filepath:
:type filepath: str
:returns: OAuth1Session
"""
# Try to find the file if no path was given
# find_keyfile returns a generator and .next() gives the first match
try:
filepath = filepath or next(find_keyfile())
except StopIteration:
raise Exception("No Keyfile found - please place keys.yaml with your tokens in the project directory or pass a custom filepath to the authorize() function")
# Load credentials from keyfile
with open(filepath, 'r') as f:
keys = yaml.load(f)
# Create authentication object
auth_object = OAuth1Session(client_key=keys["client_key"],
client_secret=keys["client_secret"],
resource_owner_key=keys["resource_owner_key"],
resource_owner_secret=keys["resource_owner_secret"])
return auth_object
评论列表
文章目录