Python 在Youtube上获取视频评论

Python 在Youtube上获取视频评论,python,youtube,google-oauth,youtube-data-api,google-api-python-client,Python,Youtube,Google Oauth,Youtube Data Api,Google Api Python Client,我使用谷歌API,特别是Youtube的数据API,使用视频ID获取视频上方的评论。我使用谷歌文档中的python示例,但我有以下错误: Missing property "redirect_uris" in a client type of "web" 这是我的客户端\u secrets\uu…json文件: { "installed": { "client_id":"xxxxxxxxxxxx-xxxxje3a7fk4jbs63g6m1a2mand4sa3h.apps

我使用谷歌API,特别是Youtube的数据API,使用视频ID获取视频上方的评论。我使用谷歌文档中的python示例,但我有以下错误:

Missing property "redirect_uris" in a client type of "web"
这是我的客户端\u secrets\uu…json文件:

{
    "installed": {
        "client_id":"xxxxxxxxxxxx-xxxxje3a7fk4jbs63g6m1a2mand4sa3h.apps.googleusercontent.com",
        "project_id":"progettotesicuozzo",
        "auth_uri":"https://accounts.google.com/o/oauth2/auth",
        "token_uri":"https://accounts.google.com/o/oauth2/token",
        "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
        "client_secret":"XXXXXXXXXXX",
        "redirect_uris":[
            "urn:ietf:wg:oauth:2.0:oob",
            "http://localhost"
        ]
    }
}
代码如下:

# Usage example:
# python test.py --videoid='<video_id>' --text='<text>'

import httplib2
import os
import sys
import json

from apiclient.discovery import build_from_document
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow

CLIENT_SECRETS_FILE = "./client_secret_....json"

YOUTUBE_READ_ONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

MISSING_CLIENT_SECRETS_MESSAGE = """WARNING: Please configure OAuth 2.0
    To make this sample run you will need to populate the client_secrets.json file found at: %s with information from the APIs Console https://console.developers.google.com
    For more information about the client_secrets.json file format, please visit: https://developers.google.com /api-client-library/python/guide/aaa_client_secrets""" % os.path.abspath(os.path.join(os.path.dirname(__file__), CLIENT_SECRETS_FILE))

def get_authenticated_service(args):
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_ONLY_SCOPE, message=MISSING_CLIENT_SECRETS_MESSAGE)

    storage = Storage("%s-oauth2.json" % sys.argv[0])
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        credentials = run_flow(flow, storage, args)

    with open("youtube-v3-discoverydocument.json", "r", encoding="utf8") as f:  
        doc = f.read()
        return build_from_document(doc, http=credentials.authorize(httplib2.Http()))

def get_comment_threads(youtube, video_id):
    results = youtube.commentThreads().list(
        part="snippet",
        videoId=video_id,
        textFormat="plainText"
    ).execute()

    for item in results["items"]:
        comment = item["snippet"]["topLevelComment"]
        author = comment["snippet"]["authorDisplayName"]
        text = comment["snippet"]["textDisplay"]
        print("Comment by " + author + ": " + text)

    return results["items"]


 def get_comments(youtube, parent_id):
    results = youtube.comments().list(
        part="snippet",
        parentId=parent_id,
        textFormat="plainText"
    ).execute()

    for item in results["items"]:
        author = item["snippet"]["authorDisplayName"]
        text = item["snippet"]["textDisplay"]
        print("Comment by " + author + ": " + text)

    return results["items"]

if __name__ == '__main__':
    # The "videoid" option specifies the YouTube video ID that uniquely
    # identifies the video for which the comment will be inserted.
    argparser.add_argument("--videoid", help="Required; ID for video for which the comment will be inserted.")
    # The "text" option specifies the text that will be used as comment.
    #argparser.add_argument("--text", help="Required; text that will be used as comment.")
    args = argparser.parse_args()

    if not args.videoid:
        exit("Please specify videoid using the --videoid= parameter.")

    youtube = get_authenticated_service(args)
    # All the available methods are used in sequence just for the sake of an example.
    try:
        video_comment_threads = get_comment_threads(youtube, args.videoid)
        parent_id = video_comment_threads[0]["id"]
        video_comments = get_comments(youtube, parent_id)
        except HttpError as e:
        print("An HTTP error " + str(e.resp.status) + " occurred:\n" +  json.dumps(str(e.content)))
    else:
        print("Inserted, listed, updated, moderated, marked and deleted comments.")

我已经使用web URL中的ID请求了一个我的个人视频,但是在提示我重定向uri之后,我得到了错误。

当您在上创建项目时,将提示您下载JSon文件。如果您似乎已删除重定向URI,则不应更改此文件。重定向URI是作为浏览器凭据的一部分创建的。需要告诉服务器将响应返回到哪里

例如:

{  
   "web":{  
      "client_id":"xxxxx-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com",
      "project_id":"daimto-tutorials-101",
      "auth_uri":"https://accounts.google.com/o/oauth2/auth",
      "token_uri":"https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
      "client_secret":"xxxxx",
      "redirect_uris":[  
         "http://localhost/google-api-php-client-samples/Analytics/Oauth2.php",
         "http://localhost/google-api-php-client-samples/Calendar/Oauth2.php"
      ],
      "javascript_origins":[  
         "https://www.example.com"
      ]
   }
}
用于web服务器请求的授权重定向URI。这 在应用程序中,用户被重定向到的路径 他们已经通过谷歌认证。路径将附加 访问的授权代码。必须有一个协议。不能 包含URL片段或相对路径。不能是公共IP 地址


权限不足意味着与您进行身份验证的用户无权执行您尝试执行的操作。您已通过用户和通道的身份验证。您只能访问该频道内的视频

这是一个不同的问题,你应该在一个问题中一次只回答一个问题。你不应该从最初的问题中删除东西,因为你不会帮助下一个人。检查我的更新。
{  
   "web":{  
      "client_id":"xxxxx-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com",
      "project_id":"daimto-tutorials-101",
      "auth_uri":"https://accounts.google.com/o/oauth2/auth",
      "token_uri":"https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
      "client_secret":"xxxxx",
      "redirect_uris":[  
         "http://localhost/google-api-php-client-samples/Analytics/Oauth2.php",
         "http://localhost/google-api-php-client-samples/Calendar/Oauth2.php"
      ],
      "javascript_origins":[  
         "https://www.example.com"
      ]
   }
}