【发布时间】:2020-09-08 00:47:54
【问题描述】:
我正在用 Python 编写代码来自动将视频添加到 YouTube 播放列表。该代码确实成功地将视频添加到播放列表,但是对于我添加的每个视频,都会提示我登录同意屏幕。对于我添加的每个视频,身份验证流程都会重复。我希望只做一次。
def insertPlaylistItem(playlistId, videoID):
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secret.json"
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_console() # Save these credentials
youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
request = youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlistId,
"position": 0,
"resourceId": {
"kind": "youtube#video",
"videoId": videoID
}
}
}
)
response = request.execute()
print(response)
client_secret.json 是这种形式:
{
"installed":{
"client_id":"XXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
"project_id":"XXXXXX",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"XXXXXXXXXXXXXXXXXXXXXXX",
"redirect_uris":[
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
有没有办法存储身份验证流程提供的这些凭据,以便以后使用?我已经通读了https://developers.google.com/youtube/v3/docs/playlists/insert,在那里我让它适用于javascript,但我只是不太明白Python的方式。我是不是对 Oauth2.0 流程理解有误?
使用 javascript,我能够以如下形式获得 token.json:
{
"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"refresh_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"scope":"https://www.googleapis.com/auth/youtube.force-ssl",
"token_type":"Bearer",
"expiry_date":1564674820338
}
【问题讨论】:
标签: python python-3.x youtube-api google-oauth