尝试将Google API与客户端密钥文件和OAuth 2.0

时间:2015-11-16 23:35:24

标签: python google-api google-api-python-client

使用 YouTube API ,特别是客户端机密文件时,我无法理解我做错了什么。

我已按照 Google开发者网站上的步骤创建客户端机密文件 -

  1. 转到https://console.developers.google.com/
  2. 转到API Manager部分
  3. 转到“凭据”标签
  4. 添加凭据按钮 - > OAuth 2.0客户端ID
  5. 申请类型 - >网络应用程序
  6. 现在可以下载客户端密钥文件了,所以我将其下载并将其放在我的电脑上
  7. 现在,我尝试运行此基本代码段,在 Google开发者网站上展示https://developers.google.com/youtube/v3/code_samples/python#rate__like__a_video

        import httplib2
        import os
        import sys
    
        from apiclient.discovery import build
        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 = "C:\\Python27\\Projects\\YoutubeBot_client_secrets.json"
    
        # This variable defines a message to display if the CLIENT_SECRETS_FILE is
        # missing.
        MISSING_CLIENT_SECRETS_MESSAGE = "something went wrong."
    
        # This OAuth 2.0 access scope allows for full read/write access to the
        # authenticated user's account.
        YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
        YOUTUBE_API_SERVICE_NAME = "youtube"
        YOUTUBE_API_VERSION = "v3"
    
        def get_authenticated_service(args):
          flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
            scope=YOUTUBE_READ_WRITE_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)
          return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
            http=credentials.authorize(httplib2.Http()))
    
        # Add the video rating. This code sets the rating to "like," but you could
        # also support an additional option that supports values of "like" and
        # "dislike."
        def like_video(youtube, video_id):
          youtube.videos().rate(
            id=video_id,
            rating="like"
          ).execute()
    
        if __name__ == "__main__":
          argparser.add_argument("--videoid", default="L-oNKK1CrnU", 
           help="ID of video to like.")
          args = argparser.parse_args()
    
          youtube = get_authenticated_service(args)
          try:
            like_video(youtube, "8hj6BK8WGtA" )
          except HttpError, e:
            print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
          else:
            print "%s has been liked." % args.videoid
    

    我刚收到有关客户端密码文件的错误(MISSING_CLIENT_SECRETS_MESSAGE错误):

    SystemExit: something went wrong.
    

    这就是根本原因 -

    ---> 29     message=MISSING_CLIENT_SECRETS_MESSAGE)
    

2 个答案:

答案 0 :(得分:1)

免责声明:这可能不是答案。

您是否尝试过这样的理智检查:

if os.path.exists(CLIENT_SECRETS_FILE):
    print('yes, the client secrets file exists')
else:
    print('the client secrets file does not exist!')

也许实际问题更令人兴奋。我只是提供这个,因为我总是犯那种愚蠢的错误...... :)

答案 1 :(得分:0)

我自己就遇到过这个问题,因为没有令人满意的答案,这就是我如何找到问题的根本原因: 删除message中的flow_from_clientsecrets参数可以为您提供更具描述性的信息:

flow = flow_from_clientsecrets(
    CLIENT_SECRETS_FILE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    # message=MISSING_CLIENT_SECRETS_MESSAGE
)

例如,我自己实现了“遗失凭证文件”错误,但由于它似乎不是强制性的,我将其删除,并得到以下错误,这有助于我解决真正的问题,如文件根据Riccati的回答,存在于文件系统中:

oauth2client.clientsecrets.InvalidClientSecretsError: Invalid file format.
See https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
Expected a JSON object with a single property for a "web" or "installed" application

注意,这不仅会解决google api的youtube组件的问题,而且还会解决所有api的问题,就像我的案例中的bigquery api一样。