python - 使用 Python 存储 Google Auth 凭据
问题描述
我从 Google API 文档中获取了相同的代码。我抓取的代码提示用户授权我的应用程序可以使用他们的帐户来点赞视频。这是代码
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
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.videos().rate(
id="eyRGEv3XAxk",
rating="like"
)
request.execute()
if __name__ == "__main__":
main()
此代码适用于有效的“客户端机密文件”。但是有什么办法让我保存凭据,这样我就不必总是提示用户授权我的应用程序?假设我想在我的代码的另一部分喜欢另一个视频,但我不想再次授权。
解决方案
使用 python pickle 库来保存和加载凭证对象。
推荐阅读
- swagger - Swagger 动态值类型
- c++ - 从 clojure 调用 C++
- php - 放大器页面 rel=canonical 指向非放大器自动 Wordpress
- java - 如何使用 Retrofit 解析复杂的 JSON 并将其放入列表中
- djaty - 设置 Djaty nodejs 时出现问题。没有发送到仪表板
- html - 在创建视差滚动效果的同时将文本固定到图像
- javascript - 带有onclick事件的功能绘图svg不起作用
- c - 指向 VLA 的指针
- weka - 在 weka 中删除或填充“NULL”以进行数据挖掘
- android - Android:可点击跨度选择文本视图中的文本