使用 youtube api 和 python 发布 youtube 评论 每次询问"authorize this application"



我使用在YouTube开发帮助网站上找到的这个示例代码来发布对视频的评论

# -*- coding: utf-8 -*-
# Sample Python code for youtube.commentThreads.insert
# 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.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 = "post.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.commentThreads().insert(
part="snippet",
body={
"snippet": {
"videoId": "VIDEO_ID",
"topLevelComment": {
"snippet": {
"textOriginal": "This is the start of a comment thread."
}
}
}
}
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()

评论是有效的,但每次它都要求我再次授权应用程序,我不知道为什么,你能帮助我吗?我得到的消息是:

"请访问此URL以授权此应用程序:URL_LINK

输入授权码:">

这是因为您没有在代码中使用刷新令牌。此刷新令牌允许您无需再次登录即可获得访问令牌。

下面的链接应该可以帮助你利用它。

Google API:使用oauth2client.client从刷新令牌获取凭据

相关内容

最新更新