Google-api-php-client: json key is missing the type field

Created on 28 Apr 2016  路  8Comments  路  Source: googleapis/google-api-php-client

I've downloaded my client_secret.json by going to URL https://console.developers.google.com/apis/credentials and clicking the 'Download JSON' option. Downloaded the JSON successfully, pasted below;

{
    "installed": {
        "client_id": "dummyID",
        "project_id": "dummy-id",
        "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"
    }
}

And my calling code;

$client = new \Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS='.CONFIG.'client_secret.json');

$client->useApplicationDefaultCredentials();
$client->setSubject('[email protected]');
$client->addScope('https://www.googleapis.com/auth/drive.file');

$service = new \Google_Service_Drive($client);
$file = new \Google_Service_Drive_DriveFile($client);

$result = $service->files->create($file, array( // Error here
    'data' => file_get_contents(SHEETS.'review.xlsx'),
    'mimeType' => 'application/vnd.google-apps.spreadsheet',
    'uploadType' => 'media'
));

This code gives me the exception

json key is missing the type field

The only reference I can find for this is here
https://github.com/google/google-auth-library-php/blob/master/src/CredentialsLoader.php#L129

Could someone tell me why I am getting this error please? It's looking for type field in my client_secret.json but I'm not sure why?

Thanks in advance.

Most helpful comment

@xorexore It turns out I was downloading the wrong client secret. You have to download the client secret for the service not the client account.

All 8 comments

Same problem here.

I tried to use:
$client->setAuthConfig('client_secret.json');

But then I got these notices (and it isn't working either):

PHP Notice:  Undefined index: client_secret in google-api-php-client/vendor/google/apiclient/src/Google/Client.php on line 862

Notice: Undefined index: client_secret in google-api-php-client/vendor/google/apiclient/src/Google/Client.php on line 862

@xorexore It turns out I was downloading the wrong client secret. You have to download the client secret for the service not the client account.

I want to get the drive's spreadsheets list. I am using the Drive api. while running apitest.php file. I face the error:
PHP Fatal error: Uncaught InvalidArgumentException: json key is missing the typ
e field in C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoader
.php:129
Stack trace:

0 C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoader.php(82)

: Google\Auth\CredentialsLoader::makeCredentials('https://www.goo...', Object(Gu
zzleHttp\Psr7\Stream))

1 C:\xampp\htdocs\googleSheets\vendor\google\auth\src\ApplicationDefaultCredent

ials.php(137): Google\Auth\CredentialsLoader::fromEnv('https://www.goo...')

2 C:\xampp\htdocs\googleSheets\vendor\google\apiclient\src\Google\Client.php(10

54): Google\Auth\ApplicationDefaultCredentials::getCredentials('https://www.goo.
..')

3 C:\xampp\htdocs\googleSheets\vendor\google\apiclient\src\Google\Client.php(22

6): Google_Client->createApplicationDefaultCredentials()

4 C:\xampp\htdocs\googleSheets\apitest.php(37): Google_Client->fetchAccessToken

WithAssertion()

5 {main}

thrown in C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoade
r.php on line 129

Fatal error: Uncaught InvalidArgumentException: json key is missing the type fie
ld in C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoader.php:
129
Stack trace:

0 C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoader.php(82)

: Google\Auth\CredentialsLoader::makeCredentials('https://www.goo...', Object(Gu
zzleHttp\Psr7\Stream))

1 C:\xampp\htdocs\googleSheets\vendor\google\auth\src\ApplicationDefaultCredent

ials.php(137): Google\Auth\CredentialsLoader::fromEnv('https://www.goo...')

2 C:\xampp\htdocs\googleSheets\vendor\google\apiclient\src\Google\Client.php(10

54): Google\Auth\ApplicationDefaultCredentials::getCredentials('https://www.goo.
..')

3 C:\xampp\htdocs\googleSheets\vendor\google\apiclient\src\Google\Client.php(22

6): Google_Client->createApplicationDefaultCredentials()

4 C:\xampp\htdocs\googleSheets\apitest.php(37): Google_Client->fetchAccessToken

WithAssertion()

5 {main}

thrown in C:\xampp\htdocs\googleSheets\vendor\google\auth\src\CredentialsLoade
r.php on line 129

@Sharma-Ravin You have probably downloaded the wrong kind of credentials. Try downloading a new set of client credentials.

from where to download to download the client secret for the service ?

@xorexore It turns out I was downloading the wrong client secret. You have to download the client secret for the service not the client account.

I'm having the same error here, but I don't understand this answer. I only have one download link on the Credentials section, and that is to the right, under the heading of "OAuth 2.0 client IDs". I downloaded the only file I could on the page as a JSON file, but still get the same error.

This worked for me, on the Account Services page I click on Furnish a new private key, this key will generate a JSON file that will contain the type field. You can check the steps here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unixkapl picture unixkapl  路  3Comments

usamamashkoor picture usamamashkoor  路  5Comments

ysaurabh33 picture ysaurabh33  路  3Comments

Romain picture Romain  路  4Comments

upendtu picture upendtu  路  4Comments