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

≯℡__Kan透↙ 提交于 2019-12-10 00:53:10

问题


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?


回答1:


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.




回答2:


Try using the header normally, but fetching the token and adding a variable containing it where you need the token to show.



来源:https://stackoverflow.com/questions/21183509/how-to-set-a-custom-header-in-google-cloud-endpoints-javascript-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!