Mahir
Mahir

Reputation: 1684

Automating 3-factor authentication through a script for the Lyft Api

I'm trying to use the Lyft rides python API to access Lyft data. Specifically, I'm trying to access the ride estimate endpoint .

from lyft_rides.auth import ClientCredentialGrant
from lyft_rides.session import Session
from lyft_rides.client import LyftRidesClient

auth_flow = ClientCredentialGrant(client_id=MY_ID, client_secret=MY_SECRET, scopes="public")
session = auth_flow.get_session()
client = LyftRidesClient(session)

response = client.get_cost_estimates(start_latitude=start_lat, start_longitude=start_long, end_latitude=end_lat, end_longitude=end_long)

However, the surge rate in the response data is always 0, even during surge hours, and I've diagnosed that it's because I'm not utilizing the 3-legged authentication.

From the lyft developer docs,

3-Legged flow for accessing user-specific endpoints.

To make ride requests or otherwise access user data, the user must grant you access. Users who don't have a Lyft account will be prompted to create a new account if they are directed through the following flow.

From the python docs,

Authorization

If you need access to a Lyft user’s account in order to make requests on their behalf, you will go through a “3-legged” flow. In this case, you will need the user to grant access to your application through the OAuth 2.0 Authorization Code flow. See Lyft API docs.

The Authorization Code flow is a two-step authorization process. The first step is having the user authorize your app and the second involves requesting an OAuth 2.0 access token from Lyft. This process is mandatory if you want to take actions on behalf of a user or access their information.

from lyft_rides.auth import AuthorizationCodeGrant
auth_flow = AuthorizationCodeGrant(
    YOUR_CLIENT_ID,
    YOUR_CLIENT_SECRET,
    YOUR_PERMISSION_SCOPES,
)
auth_url = auth_flow.get_authorization_url()

Navigate the user to the auth_url where they can grant access to your application. After, they will be redirected to a redirect_url with the format REDIRECT_URL?code=UNIQUE_AUTH_CODE. Use this redirect_url to create a session and start LyftRidesClient.

session = auth_flow.get_session(redirect_url)
client = LyftRidesClient(session)
credentials = session.oauth2credential

Keep credentials information in a secure data store and reuse them to make API calls on behalf of your user. The SDK will handle the token refresh for you automatically when it makes API requests with a LyftRidesClient.

Question

I'm trying to automate the python request within a script. Given that the 3rd leg of the authentication requires manually visiting a url and obtaining a code, is it possible to do this through a script?

Upvotes: 1

Views: 704

Answers (1)

acityinohio
acityinohio

Reputation: 186

[Full Disclosure: I'm one of Lyft's Developer Advocates]

The only way to get that data is by requesting therides.request scope is through the 3-legged OAuth flow (sorry about that). However, you only need to request this external authorization once if you ask for the offline scope as part of the initial authorization. If you have that scope requested initially, you can use refresh_tokens as outlined here and not get prompted for the external URL:

https://developer.lyft.com/docs/authentication#section-step-5-refreshing-the-access-token

If you're only using this script locally I'd recommend going through this authorization once and then building in refresh token logic into your script if your token has expired. Hope that helps!

Upvotes: 1

Related Questions