2

I'm building a Chrome app, and I'd really like it to communicate with my server over cloud endpoints, but there are two problems that I'm not sure how to overcome:

  1. The Google apis javascript library is an external script, which I can't use in a page in a chrome app. Can I just copy the source of the library, and place it in a file in my app's source?
  2. The client checks your javascript origin, but an extension's origin is of the form chrome-extension://EXTENSION_ID, which the developer console doesn't accept as a javascript origin.

How do I get around these issues?

bigblind
  • 12,539
  • 14
  • 68
  • 123

2 Answers2

3

1. Loading the gapi client

Indeed the only way I have found to load the gapi client is to use a webview, as explained here. I have tested itand it works well but weirdly the authentication does not work at all, and Cloud Endpoints believes you're anonymous.

In addition communication from a webview to the rest of the world is pretty tricky (window.postMessage does not allow to send a response in a callback).

I think you will be better off calling directly the REST methods using AJAX requests, with a helper such as jQuery or other. You just have to set the Authorization header using you access token, like this in jQuery :

 $.ajax({
            type:"GET",
            beforeSend: function (request)
            {
                request.setRequestHeader("Authorization", "Bearer "+THE_ACCESS_TOKEN);
            },
            url: "https://yourapp.appspot.com/_ah/api/yourapi/v1/yourmethod",
            success: function(msg) {
                //Put here your callback
            }
    });

See below how to get the access token.

2. Authorization in Chrome Apps

You do not have to worry about the origin part in Chrome Apps, you simply need to generate a client id specific to the Chrome App, and use the Chrome Identity API to get authorization from the user. Check the Chrome Identity API documentation for more details.

Note that since you will need to create a new client id, you will need to update your Google Cloud Endpoint's configuration to add this client id to the list of authorized clients.

Community
  • 1
  • 1
David
  • 5,481
  • 2
  • 20
  • 33
2
  1. Yes. There are several GAPI packages for Angular in bower.

  2. The Tasks sample app says it uses the GAPI. Check the example there: https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/tasks

Hope that helps!

Michael Cole
  • 15,473
  • 7
  • 79
  • 96