python - 通过 Lyft Api 脚本自动执行三因素身份验证

标签 python authentication oauth lyft-api

我正在尝试使用 Lyft rides用于访问 Lyft 数据的 python API。具体来说,我正在尝试访问 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)

但是,即使在高峰时段,响应数据中的激增率也始终为 0,我诊断出这是因为我没有使用 3 足身份验证。

来自lyft developer docs ,

用于访问特定于用户的端点的三足流程。

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.

来自python docs ,

授权

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.

问题

我正在尝试在脚本中自动执行 python 请求。鉴于身份验证的第三步需要手动访问 url 并获取代码,是否可以通过脚本来完成此操作?

最佳答案

[全面披露:我是 Lyft 的开发者倡导者之一]

获取该数据的唯一方法是通过 3 足 OAuth 流程请求rides.request范围(对此感到抱歉)。但是,如果您请求 offline 范围作为初始授权的一部分,则只需请求此外部授权一次。如果您最初请求了该范围,则可以使用此处概述的 refresh_token,而不会收到输入外部 URL 的提示:

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

如果您仅在本地使用此脚本,我建议您执行一次此授权,然后在您的 token 已过期时将刷新 token 逻辑构建到您的脚本中。希望有帮助!

关于python - 通过 Lyft Api 脚本自动执行三因素身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43645095/

相关文章:

c# - C# 中的 mysql 查询登录

web-services - iOS 4.3 - 尝试 Web 服务连接时网络连接丢失?

grails - Google 同意屏幕未按预期显示

python - Pandas 中的 Groupby 和累计计数

python - 是否可以将 `first()` 和 `last()` 应用于 Pandas groupby 操作中的分隔列?

python - Pytesseract 没有给出预期的输出

java - 如何通过登录注销用户(非当前)?

oauth - 使用 Scribe 的 google api 单点登录?

javascript - 保护用于 Javascript 小部件的 API

python - 无法在 python 3.7.0 上导入 ijson 模块