简体   繁体   中英

Google API client library in javascript not working locally

I tried to run the sample given here on my web server which is Google App Engine server.

I modified the clientId and apiKey with clientId and clientSecret from my google cloud console.

When I run, I see in javascript console a 400 Bad Request for

https://content.googleapis.com/discovery/v1/apis/plus/v1/rpc?fields=methods%2F *%2Fid&pp=0&key=TO6faADFEmHdfdh08mm8XgR

Please guide me.

That is a pretty old sample! Try https://developers.google.com/+/quickstart/javascript for a newer one.

With regards to this problem: don't put your client secret in the key field! The client secret should never be put into public, it should only ever be used on a server where it can be kept private. The API key in this case is a separate entry (the simple API access browser key).

More about API keys: https://developers.google.com/console/help/#generatingdevkeys

As far as I can tell, the sample is working on the server you linked. As Ian mentioned, the sample you are running looks a little outdated, the JavaScript Quickstart is the latest and greatest and uses the Google+ Sign-In button which has a number of enhancements, particularly when you are using client-side authorization.

To test that your sample is working, you can open the Chrome developer tools and run some simple client library code such as:

gapi.client.plus.activities.list(
  {userId: 'me'}).execute(
    function(resp){
      console.log(resp);
    });

If you see your activity feed, everything is working.

Regarding locally ... In order for the JavaScript library to work, you also must be running a web server on your computer such as the Python Simple HTTP Server:

python -m SimpleHTTPServer 8000

or Apache, twistd, etc.

If you directly open the file, permissions will prevent the library from working.

Your authorized JavaScript origins must also match the port (and protocol, eg https/http) that are set up in your Google APIs console project. If it's working remotely but not locally, you might be missing a localhost origin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM