def main(): """ Execution function. """ # starting Yelp API session session = OAuth2Session(api_id, api_secret, api_token) # Grab the titles for our missing data missed_reviews = glob.glob('data/*missed_critic.json') missed_stars = glob.glob('data/*_missed.json') # Re-run our intial pull code initial_pull() # add all of the items that were overlooked during first pull for establishment in missed_reviews: name = _grab_city_name( establishment) # need to know which city we're working with # open the file that contains the missed values with open(establishment, 'r') as f: new_data = json.loads(f.read()) aug_yelp_data(session, name, new_data) # open the list of stars that weren't in initial Yelp API pull for locale in missed_stars: name = _grab_city_name(locale) with open(locale, 'r') as f: new_stars = json.loads(f.read()) aug_yelp_data(session, name, new_stars) return
def initial_pull(): """ Primary execution for data pulls. """ # Instantiate the OAuth2 session to access Yelp data session = OAuth2Session(api_id, api_secret, api_token) ## Use a loop to pull data for each city based on parameters for city in cities: json_data = grab_data(session, city) # quickly saving each data file as a json so I can check share it with colleagues with open('data/{}.json'.format(city), 'w') as d: for item in json_data: json.dump(item, d, indent=4) # I will now add the data to my local MongoDB instance mongo_dump(city, json_data) # To make sure the data is as robust as possible, I'm going to run a different # search query and have it sorted by 'review_count', then add any unique values # to the MongoDB distribution_center for city in cities: addl_data = grab_data(session, city, sort='review_count') # check to see if the values are already in our database, if they aren't # add them as new documents in their respective collections insert_unique(city, addl_data) return
def _start_session(self): if self.auth_client.access_token is None: self.auth_client.refresh(refresh_token=self.refresh_token) self.session = OAuth2Session( client_id=self.auth_client.client_id, client_secret=self.auth_client.client_secret, access_token=self.auth_client.access_token, )
def get_session(self, access_token=None): client_id = self.consumer_id, client_secret = self.consumer_secret, return OAuth2Session( client_id, client_secret, #access_token=access_token).get('http://localhost:5000/access_token') access_token=access_token).get( 'https://www.googleapis.com/oauth2/v3/userinfo').json()
def friends(): """Gets the friends of the current user""" # check to make sure the user authorized the request fb_creds = application.config['OAUTH_CREDENTIALS']['facebook'] oauth = OAuth2Session(fb_creds['id'], fb_creds['secret'], session['access_token']) f = oauth.get('https://graph.facebook.com/me/friends').json() # TODO: handle paging of users users = f['data'] return json.dumps(users)
def create_session(self): if self.client_secret and self.client_id and self.access_token: session = OAuth2Session( client_id=self.client_id, client_secret=self.client_secret, access_token=self.access_token ) self.session = session else: raise QuickbooksException("Quickbooks authenication fields not set. Cannot create session.") return self.session
def fetch_blenderid_user() -> dict: """Returns the user info of the currently logged in user from BlenderID. Returns an empty dict if communication fails. Example dict: { "email": "*****@*****.**", "full_name": "dr. Sybren A. St\u00fcvel", "id": 5555, "roles": { "admin": true, "bfct_trainer": false, "cloud_single_member": true, "conference_speaker": true, "network_member": true } } """ import httplib2 # used by the oauth2 package bid_url = '%s/api/user' % blender_id_endpoint() log.debug('Fetching user info from %s', bid_url) try: client_id = current_app.config['OAUTH_CREDENTIALS']['blender-id']['id'] client_secret = current_app.config['OAUTH_CREDENTIALS']['blender-id'][ 'secret'] oauth_session = OAuth2Session( client_id, client_secret, access_token=session['blender_id_oauth_token']) bid_resp = oauth_session.get(bid_url) except httplib2.HttpLib2Error: log.exception('Error getting %s from BlenderID', bid_url) return {} if bid_resp.status_code != 200: log.warning('Error %i from BlenderID %s: %s', bid_resp.status_code, bid_url, bid_resp.data) return {} if not bid_resp.json(): log.warning('Empty data returned from BlenderID %s', bid_url) return {} log.debug('BlenderID returned %s', bid_resp.json()) return bid_resp.json()
def post_data(self, endpoint, data): self.url = urljoin('https://api.service.hmrc.gov.uk', endpoint) self.data = data def expired(): if int(time.time()) > session['tokenTTL']: return True else: return False if not expired(): access_token = session['access_token'] else: refresh_token_response = self.service.get_raw_access_token( data={'grant_type': 'refresh_token', 'refresh_token': session['refresh_token'] } ) r = refresh_token_response.json() for k, v in r.items(): if k in ("expires_in"): session['tokenTTL'] = int(time.time()) + int(v) else: session[k] = v access_token = session['access_token'] headers = {"Accept":"application/vnd.hmrc.1.0+json", "Content-Type": "application/json", "Authorization": "Bearer %s" % access_token } s = OAuth2Session(client_id=self.client_id, client_secret=self.client_secret, access_token=access_token) response = s.post(self.url, json=self.data, headers=headers) return response
def get_data(self, endpoint, params=None): self.url = urljoin('https://api.service.hmrc.gov.uk', endpoint) self.headers =headers = {"Accept":"application/vnd.hmrc.1.0+json", "Scope": "read:vat"} self.params = params def expired(): if int(time.time()) > session['tokenTTL']: return True else: return False if not expired(): access_token = session['access_token'] else: refresh_token_response = self.service.get_raw_access_token( data={'grant_type': 'refresh_token', 'refresh_token': session['refresh_token'] } ) r = refresh_token_response.json() for k, v in r.items(): if k in ("expires_in"): session['tokenTTL'] = int(time.time()) + int(v) else: session[k] = v access_token = session['access_token'] s = OAuth2Session(client_id=self.client_id, client_secret=self.client_secret, access_token=access_token) response = s.request(method='GET', url=self.url, bearer_auth=True, headers=headers, params=params) return response
def start_session(self): if not self.started: if self.client_id == '': raise QuickbooksException( "Client Id missing. Cannot create session.") if self.client_secret == '': raise QuickbooksException( "Client Secret missing. Cannot create session.") if self.access_token == '': raise QuickbooksException( "Access Token missing. Cannot create session.") self.session = OAuth2Session( client_id=self.client_id, client_secret=self.client_secret, access_token=self.access_token, ) self.started = True return self.session
def __init__(self, access_token, client_id, client_secret): self.requests_client = OAuth2Session(client_id=client_id, client_secret=client_secret, access_token=access_token)
def fetch_blenderid_user() -> dict: """Returns the user info of the currently logged in user from BlenderID. Returns an empty dict if communication fails. Example dict: { "email": "*****@*****.**", "full_name": "dr. Sybren A. St\u00fcvel", "id": 5555, "roles": { "admin": true, "bfct_trainer": false, "cloud_has_subscription": true, "cloud_subscriber": true, "conference_speaker": true, "network_member": true } } :raises LogoutUser: when Blender ID tells us the current token is invalid, and the user should be logged out. """ import httplib2 # used by the oauth2 package my_log = log.getChild('fetch_blenderid_user') bid_url = urljoin(current_app.config['BLENDER_ID_ENDPOINT'], 'api/user') my_log.debug('Fetching user info from %s', bid_url) credentials = current_app.config['OAUTH_CREDENTIALS']['blender-id'] oauth_token = session.get('blender_id_oauth_token') if not oauth_token: my_log.warning('no Blender ID oauth token found in user session') return {} assert isinstance(oauth_token, str), f'oauth token must be str, not {type(oauth_token)}' oauth_session = OAuth2Session(credentials['id'], credentials['secret'], access_token=oauth_token) try: bid_resp = oauth_session.get(bid_url) except httplib2.HttpLib2Error: my_log.exception('Error getting %s from BlenderID', bid_url) return {} if bid_resp.status_code == 403: my_log.warning('Error %i from BlenderID %s, logging out user', bid_resp.status_code, bid_url) raise LogoutUser() if bid_resp.status_code != 200: my_log.warning('Error %i from BlenderID %s: %s', bid_resp.status_code, bid_url, bid_resp.text) return {} payload = bid_resp.json() if not payload: my_log.warning('Empty data returned from BlenderID %s', bid_url) return {} my_log.debug('BlenderID returned %s', payload) return payload
#Se debe decodificar para que sea un string "Authorization": "Basic " + authString.decode() } params = {"grant_type": "client_credentials"} r = requests.post('https://oauth.brightcove.com/v4/access_token', headers=headersMap, params=params).json( ) #Se hace una petición POST para conseguir el token return r['access_token'] #Se regresa solamente el token def getAllVideos(session): r = session.get( 'https://cms.api.brightcove.com/v1/accounts/6044537239001/playlists', params={'format': 'json'} ) # Se hace un HTTP GET en la sesion ya autorizada del url y params se formatea en json with open('datap.json', 'w') as f: #Paso el json conseguido(r) a un archivo llamado data.json json.dump(r.json(), f) #se consigue el token token = getToken(client_id, client_secret) #La session se inicializa con los datos anteriores session = OAuth2Session(client_id, client_secret, token) getAllVideos(session=session)
def recreate_session(access_token): return OAuth2Session(HumanAPI.client_id, HumanAPI.client_secret, access_token, HumanAPI)
def __init__(self, client_id, client_secret, access_token=None): self.session = OAuth2Session(client_id, client_secret, access_token) self.call_url = None if access_token: self.set_access_token(access_token)
def __init__(self, client_id, client_secret, access_token=None): self.session = OAuth2Session(client_id=client_id, client_secret=client_secret, access_token=access_token)
def get_session(self): return OAuth2Session(self.client_id, self.client_secret, self.token.access_token, service=self.service)