0

I downloaded the gsuite developers python code from :

(https://github.com/gsuitedevs/python-samples)

I then enabled api access and downloaded the credentials.json file and ran the quickstart.py in:

(python-samples-master/slides/quickstart) and it worked and outputted

The presentation contains 5 slides:
- Slide #1 contains 4 elements.
- Slide #2 contains 11 elements.
- Slide #3 contains 9 elements.
- Slide #4 contains 5 elements.
- Slide #5 contains 12 elements.

So it worked. Then I tried to run test_snippets.py in:

(python-samples-master/slides/snippets)

And I get an error

======================================================================
ERROR: setUpClass (__main__.SnippetsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1228, in _implicit_credentials_from_files
    credentials_filename)
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1397, in _get_application_default_credential_from_file
    AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)")
oauth2client.client.ApplicationDefaultCredentialsError: 'type' field should be defined (and have one of the 'authorized_user' or 'service_account' values)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_snippets.py", line 32, in setUpClass
    super(SnippetsTest, cls).setUpClass()
  File "~/Desktop/python-samples-master/slides/snippets/base_test.py", line 27, in setUpClass
    cls.credentials = cls.create_credentials()
  File "~/Desktop/python-samples-master/slides/snippets/base_test.py", line 44, in create_credentials
    credentials = GoogleCredentials.get_application_default()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1271, in get_application_default
    return GoogleCredentials._get_implicit_credentials()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1256, in _get_implicit_credentials
    credentials = checker()
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1231, in _implicit_credentials_from_files
    extra_help, error)
  File "~/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 1429, in _raise_exception_for_reading_json
    credential_file + extra_help + ': ' + str(error))
oauth2client.client.ApplicationDefaultCredentialsError: An error was encountered while reading json file: ~/Documents/credentials/credentials.json (pointed to by GOOGLE_APPLICATION_CREDENTIALS environment variable): 'type' field should be defined (and have one of the 'authorized_user' or 'service_account' values)

I definitely have a GOOGLE_APPLICATION_CREDENTIALS that points to the same credentials that successfully ran quickstart.py.

Is there something else I need or do I need to change some code to load the credentials data in?

It seems like GoogleCredentials.get_application_default() call is the one that is erroring

    def create_credentials(cls):
        credentials = GoogleCredentials.get_application_default()
        scope = [
            'https://www.googleapis.com/auth/drive',
        ]
        return credentials.create_scoped(scope)
John Hanley
  • 74,467
  • 6
  • 95
  • 159
Mike
  • 227
  • 1
  • 3
  • 17

1 Answers1

0

The file pointed to the environment variable GOOGLE_APPLICATION_CREDENTIALS is not a valid service account json file.

Open your service account json file. The beginning of the file should look similar to this:

{
  "type": "service_account",
  "project_id": "development-123456",
  "private_key_id": "19c38bac6560abcdef01234567ac4da7991cbaad",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB
John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • Ok, yes, I just got a new service account json file and the format looks like the one you posted with some other information as well. – Mike Mar 14 '19 at 00:19