python - 向 YouTube v3 Data API 发出请求时,我在哪里添加我的授权令牌?
问题描述
我目前正在使用 Python Requests 模块将视频添加到我创建的播放列表中。在 docs 上playlistItems.insert
,它说需要对三个可能的范围之一进行授权。我在项目的凭据面板中创建了 OAUTH2.0 凭据令牌并正确设置了范围。目前我正在尝试按如下方式传递凭证:
payload = {
'access_token': [My Client ID],
'part': 'snippet'}
new_vid = requests.post(f'{base_url}playlistItems', params=payload)
执行代码时,我收到以下错误消息:
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."
我是在正确的地方传递授权令牌,还是应该在POST
请求中的其他地方传递它?
解决方案
您可以将授权令牌作为标头参数传递。检查以下示例:
import requests
headers = {
'Authorization': 'Bearer [YOUR_ACCESS_TOKEN]',
'Accept': 'application/json',
'Content-Type': 'application/json',
}
params = (
('key', '[YOUR_API_KEY]'),
('part','snippet')
)
response = requests.post('https://youtube.googleapis.com/youtube/v3/playlistItems', headers=headers, params=params)
推荐阅读
- java - 无法加载 JNotify 本机库(java.library.path 中没有 jnotify)
- java - 如何使用 SpringJUnit4ClassRunner 将映射器(mapstruct)注入到 Junit 测试中
- python - 如何使用python用单词解决行列式矩阵?在python中使用单词x如何解决?
- git - 了解 Visual Studio 中的 git 分支
- python-3.x - 性能问题 boto3 kms 解密方法
- email - PHPMailer 和 DKIM,使用经过身份验证的 SMTP 服务器进行 SPF 验证
- c++ - C++ 中的处理时间
- c# - 如何将透明的winform设置为面板透明面板,以显示为背景?
- php - 如何使用 phpunit 在 PHP 中模拟 SimpleXMLElement?
- winapi - 使用 `HttpSendRequestW` 发送长度为 30 的字符串,但 Dart 服务器只接收 8