1
votes

I am trying to use the javascript sdk to do an oauth login and access the google plus api. Basically the same code here: https://developers.google.com/api-client-library/javascript/features/authentication

In my firebug console, this is the url that is sending the api request to: https://content.googleapis.com/discovery/v1/apis/plus/v1/rest?fields=servicePath%2Cresources%2Cparameters%2Cmethods&pp=0&key={key}

This is the error that comes back: {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}}

I have: 1. Added Google Plus Api to my project 2. Created oauth credentials 3. Setup my consent screen

However, I am still getting the error.

1
Is {key} what you actually see in the request? Or did you replace your actual key with that for this questions? - abraham
I replaced key for this question - tnguyen444

1 Answers

3
votes

The reason is that you have the key defined in the request. As specified in the discovery API docs (https://developers.google.com/discovery/v1/getting_started#before_starting):

"The APIs Discovery Service provides only public methods that do not require authentication. In addition, unlike the requests you make to many other Google APIs, the requests you make to the Discovery Service API should not include an API key. If you do provide a key, the requests will fail. This behavior helps ensure that you don't accidentally disclose your API key when distributing tools that are based on the Google APIs Discovery Service."

So you can solve the problem by removing the key from your request entirely.

If you are using Google's javascript client to do this and the error occurs when loading further APIs, you have to unset the key first:

gapi.client.setApiKey( null );          
gapi.client.load( "plus", "v1", function( apiresponse ) { ... } );

If another function requires the key later, you have to set it again.

To avoid setting and unsetting the key constantly, I load all the needed APIs before authentication, then set the API key and thus will no longer have the issue.