def authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All your app credentials and information imported from the configuration file. storage_filename (str) Filename to store OAuth 2.0 Credentials. Returns (UberRidesClient) An UberRidesClient with OAuth 2.0 Credentials. """ auth_flow = AuthorizationCodeGrant( credentials.get('client_id'), credentials.get('scopes'), credentials.get('client_secret'), credentials.get('redirect_url'), ) auth_url = auth_flow.get_authorization_url() login_message = 'Login and grant access by going to:\n{}\n' login_message = login_message.format(auth_url) response_print(login_message) redirect_url = 'Copy the URL you are redirected to and paste here: \n' result = raw_input(redirect_url).strip() try: session = auth_flow.get_session(result) except (ClientError, UberIllegalState), error: fail_print(error) return
def authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All your app credentials and information imported from the configuration file. storage_filename (str) Filename to store OAuth 2.0 Credentials. Returns (UberRidesClient) An UberRidesClient with OAuth 2.0 Credentials. """ auth_flow = AuthorizationCodeGrant( credentials.get('client_id'), credentials.get('scopes'), credentials.get('client_secret'), credentials.get('redirect_url'), ) auth_url = auth_flow.get_authorization_url() login_message = 'Login and grant access by going to:\n{}\n' login_message = login_message.format(auth_url) response_print(login_message) redirect_url = 'Copy the URL you are redirected to and paste here: \n' result = input(redirect_url).strip() try: session = auth_flow.get_session(result) except (ClientError, UberIllegalState) as error: fail_print(error) return credential = session.oauth2credential credential_data = { 'client_id': credential.client_id, 'redirect_url': credential.redirect_url, 'access_token': credential.access_token, 'expires_in_seconds': credential.expires_in_seconds, 'scopes': list(credential.scopes), 'grant_type': credential.grant_type, 'client_secret': credential.client_secret, 'refresh_token': credential.refresh_token, } with open(storage_filename, 'w') as yaml_file: yaml_file.write(safe_dump(credential_data, default_flow_style=False)) return UberRidesClient(session, sandbox_mode=True)
def request_surge_ride(api_client, surge_confirmation_id=None): """Use an UberRidesClient to request a ride and print the results. If the product has a surge_multiple greater than or equal to 2.0, a SurgeError is raised. Confirm surge by visiting the surge_confirmation_url and automatically try the request again. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. surge_confirmation_id (string) Unique identifer received after confirming surge. Returns The unique ID of the requested ride. """ try: request = api_client.request_ride( product_id=SURGE_PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, surge_confirmation_id=surge_confirmation_id, seat_count=2 ) except SurgeError as e: surge_message = 'Confirm surge by visiting: \n{}\n' surge_message = surge_message.format(e.surge_confirmation_href) response_print(surge_message) confirm_url = 'Copy the URL you are redirected to and paste here: \n' result = input(confirm_url).strip() querystring = urlparse(result).query query_params = parse_qs(querystring) surge_id = query_params.get('surge_confirmation_id')[0] # automatically try request again return request_surge_ride(api_client, surge_id) except (ClientError, ServerError) as error: fail_print(error) return else: success_print(request.json) return request.json.get('request_id')
def request_ride(api_client, surge_confirmation_id=None): """Use an UberRidesClient to request a ride and print the results. If the product has a surge_multiple greater than or equal to 2.0, a SurgeError is raised. Confirm surge by visiting the surge_confirmation_url and automatically try the request again. Parameters api_client (UberRidesClient) An authorized UberRidesClient to access API resources. surge_confirmation_id (string) Unique identifer received after confirming surge. Returns """ try: request = api_client.request_ride( PRODUCT_ID, START_LAT, START_LNG, END_LAT, END_LNG, surge_confirmation_id=surge_confirmation_id, ) except SurgeError, e: surge_message = 'Confirm surge by visiting: \n{}\n' surge_message = surge_message.format(e.surge_confirmation_href) response_print(surge_message) confirm_url = 'Copy the URL you are redirected to and paste here: \n' result = raw_input(confirm_url).strip() querystring = urlparse(result).query query_params = parse_qs(querystring) surge_id = query_params.get('surge_confirmation_id')[0] # automatically try request again return request_ride(api_client, surge_id)