【问题标题】:How to use Udemy api using requests module in python如何在 python 中使用 requests 模块使用 Udemy api
【发布时间】:2021-04-09 12:02:30
【问题描述】:

我正在尝试从 udemy api 获取一些数据。当我提出 GET 请求时,我得到 403 状态码

我尝试了什么:

import requests

headers = {
    'Authorization': 'Basic {BASE64_ENCODED(CLIENT_ID:CLIENT_SECRET)}',
}

response = requests.get('https://www.udemy.com/api-2.0/courses/', headers=headers)

我也尝试过使用 base64encode 和 auth,但也没有用。

根据 Udemy,

要发送经过身份验证的请求,请将 client_id 和 client_secret 值作为 base64 编码的 HTTP 授权标头提供。

curl --user {YOUR_CLIENT_ID}:{YOUR_CLIENT_SECRET} https://www.udemy.com/api-2.0/courses/ curl -H "Authorization: Basic {BASE64_ENCODED(CLIENT_ID:CLIENT_SECRET)}" https://www.udemy.com/api-2.0/courses/

提前感谢你们的帮助!

【问题讨论】:

标签: python api oauth python-requests


【解决方案1】:

嗯,那是因为你发送的字符串是:

"Basic {BASE64_ENCODED(CLIENT_ID:CLIENT_SECRET)}"

您没有传递实际的 base64 编码参数。 对于初学者,导入 base64 然后用它来编码你的 client_id 和你的 client_secret,你应该能够弄清楚如何从 Udemy 的 API 文档中获取这些,最终它应该看起来像这样:

import requests
import base64

client_id = <client_id you obtain from Udemy>
client_secret = <client_secret you obtain from Udemy>

client_id_secret = f"{client_id}:{client_secret}"

b64_client_id_secret = base64.encode(client_id_secret)

headers = 
                  {
                    'Authorization': f'Basic {b64_client_id_secret}'
                  }

【讨论】:

  • 使用 encode() 我得到一个TypeError: encode() missing 1 required positional argument 'output'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多