Python
This tutorial walks you through the steps to implement the Driver API in your Python application. The Python SDK is an easy way to add Uber functionality into your app. You’ll set up and configure the SDK, and then work through an example Driver integration.
¶ Install the SDK
Once your app is registered on the developer dashboard, you’re ready to install the SDK. To install, run:
pip install uber-rides
The Python SDK is open source, so you can find it on Github.
¶ Create an Uber session
Once you’ve created your app, you’ll be given a client_id
and client_secret
. These are used to authenticate your application and the driver when calling the API.
The following endpoints require a driver access token:
¶ Authorize a driver for your application
To fetch a driver’s profile, trips, or payments a driver must grant access to your application through the OAuth 2.0 Authorization Code flow. See the Uber API docs on authentication for more information.
from uber_rides.auth import AuthorizationCodeGrant
auth_flow = AuthorizationCodeGrant(
<CLIENT_ID>,
<SCOPES>,
<CLIENT_SECRET>,
<REDIRECT_URI>
)
auth_url = auth_flow.get_authorization_url()
Replace the values of each placeholder above (<CLIENT_ID>
, <CLIENT_SECRET>
,<REDIRECT_URI>
and <SCOPES>
). You can find these values in your developer dashboard under the your application’s Auth tab. The <REDIRECT_URI>
must match the one of the values listed in your dashboard.
Have drivers sign in and use the auth_url
to grant access to your application. After authorization is complete, drivers are redirected to the redirect_url
. The redirect_url
uses this format: <REDIRECT_URI>?code=UNIQUE_AUTH_CODE
. The redirect_url
creates a session and starts the UberRidesClient.
from uber_rides.session import Session
from uber_rides.client import UberRidesClient
session = auth_flow.get_session(redirect_url)
client = UberRidesClient(session, sandbox_mode=True)
credentials = session.oauth2credential
Make sure to store credentials
information in a secure data store so drivers don’t need to go through the authorization processes repeatedly. The SDK handles token refreshing automatically when it makes API requests with an UberRidesClient.
¶ Fetch a drivers profile
response = client.get_driver_profile()
profile = response.json
first_name = profile.get('first_name')
last_name = profile.get('last_name')
email = profile.get('email')
¶ Fetch a drivers trips
response = client.get_driver_trips()
trips = response.json
¶ Fetch a drivers payments
response = client.get_driver_payments()
payments = response.json