Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a custom header in Google Cloud Endpoints Javascript Client?

I can fetch a list of blog posts from Google Cloud Endpoints using the Javascript Client:

gapi.client.blog.posts.list().execute(function (resp) {
  console.log(resp);
});

But I need to set a custom header value in the Google Cloud Endpoints request that contains a user token (this could be an access token from Facebook). How can I do that using the Javascript Client from Google? I could solve this by not using the Javascript Client from Google, but I would rather use it.

https://developers.google.com/appengine/docs/java/endpoints/consume_js https://developers.google.com/api-client-library/javascript/reference/referencedocs

edit

It seems I can pass the custom header value like this:

gapi.auth.setToken({
    access_token: 'this is my custom value'
});

Doesn't seem good practice though. Is there a better way to do this?

like image 511
Korneel Avatar asked Jan 17 '14 10:01

Korneel


1 Answers

You can now do this using gapi.client.request, for example:

gapi.client.init({
    'clientId': 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
    'scope': 'your_scope'
}).then(function() {
    return gapi.client.request({
        'path': 'http://path/to/your/endpoints/api',
        'headers': { 'mycustomheader': 'myvalue' }
    })
}).then(function(response) {
    console.log(response.result);
}, function(reason) {
    console.log('Error: ' + reason.result.error.message);
});

See also the Getting Started page of the Google API Javascript Client documentation.

like image 133
Adam Avatar answered Oct 03 '22 15:10

Adam