2

我使用Google API,特別是Youtube的Data API,使用視頻ID在視頻上方獲得評論。我使用Python的例子來自谷歌文檔here,但我下面的錯誤:在Youtube上獲得視頻評論

Missing property "redirect_uris" in a client type of "web" 

所以,這是我的CLIENT_SECRETS _.... 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.") 

如何配置好我的json文件?

固定我的JSON文件後,我現在面臨着這樣的錯誤:

An HTTP error 403 occurred: 
{ 
    "error": { 
     "errors": [ 
      { 
       "domain": "global", 
       "reason": "insufficientPermissions", 
       "message": "Insufficient Permission" 
      } 
     ], 
     "code": 403, 
     "message": "Insufficient Permission" 
    } 
} 

我請求我的個人視頻,使用Web URL的ID,但我得到了錯誤,我以後已經被提示到redirect_uri。

回答

2

當您在google developer console上創建項目時,系統將提示您下載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" 
     ] 
    } 
} 

Authorized redirect URIs For use with requests from a web server. This is the path in your application that users are redirected to after they have authenticated with Google. The path will be appended with the authorization code for access. Must have a protocol. Cannot contain URL fragments or relative paths. Cannot be a public IP address.

insufficientPermissions意味着你與認證用戶不能訪問做你正在嘗試做的。您通過用戶和頻道進行身份驗證。您只能訪問該頻道中的視頻。

+0

我已經更新了我的文章:) –

+0

這是一個不同的問題,你應該堅持一個問題在一個問題。你不應該從原來的問題中刪除你不會幫助下一個人的東西。檢查我的更新。 – DaImTo

相關問題