golosovsky
golosovsky

Reputation: 718

getting an error when trying to use Google API with Client Secrets File and OAuth 2.0

I just can't understand what am I doing wrong when using YouTube API, and specifically the client secrets file.

I've created a client secrets file by following the steps on the google developers website —

  1. go to https://console.developers.google.com/
  2. go to API Manager section
  3. go to Credentials tab
  4. Add Credentials button -> OAuth 2.0 Client ID
  5. Application type -> Web Application
  6. The client secret file is ready to be downloaded now, so I download it and place it on my pc

Now, I try to run this basic code snippet, presented on the google developers website 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

And I just receive an error (the MISSING_CLIENT_SECRETS_MESSAGE error) regarding my client secret file:

SystemExit: something went wrong.

And that's the root cause -

---> 29     message=MISSING_CLIENT_SECRETS_MESSAGE)

Upvotes: 2

Views: 12259

Answers (3)

Kamalnath J
Kamalnath J

Reputation: 1

Check if it is service account key or client secret key. client secret key should start with {"installed":{"client_id"............

Upvotes: 0

C. S.
C. S.

Reputation: 103

I just had this problem myself, and as there is no satisfactory answer, here is how I got to the root cause of the problem: Removing the message parameter in the flow_from_clientsecrets should get you a more descriptive message:

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

For example, I had the "missing credentials file" error implemented myself, but as it don't seem to be mandatory I removed it, and got the following error, which helped me get to the real problem, as the file existed in the filesystem, according to the answer of 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

Note that this does not only account for problems with the youtube component of the google api, but rather all api's, as in my case the bigquery api.

Upvotes: 0

Riccati
Riccati

Reputation: 461

DISCLAIMER: This is probably not an answer.

Have you tried something like this as a sanity check:

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

Perhaps the actual problem is more exciting. I'm only offering this because I make that kind of dumb mistake all the time... :)

Upvotes: -1

Related Questions