【问题标题】:Getting Youtube Channel ID using oAuth2.0 with Django/Python使用 oAuth2.0 和 Django/Python 获取 Youtube 频道 ID
【发布时间】:2021-09-09 15:45:22
【问题描述】:
有没有办法使用 Google oAuth2 获取 youtube 频道 ID 或 youtube 用户 ID。
我可以使用 oauth 检索“个人资料”和电子邮件详细信息。还提供了“youtube-readonly”的范围。
我想使用 Python/Django 实现这一目标
注意:这里的一般想法是显示登录用户的 Youtube 频道名称和订阅者数量。 [用户仅通过 Google OAuth 登录。]
【问题讨论】:
标签:
python
django
oauth-2.0
youtube-api
【解决方案1】:
终于找到了用python实现的方法。
希望这对有类似问题的人有所帮助。
# -*- coding: utf-8 -*-
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.channels().list(
part="id,status,snippet,statistics",
mine=True
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()