如何在Google Cloud Endpoints Javascript客户端中设置自定义标题? [英] How to set a custom header in Google Cloud Endpoints Javascript Client?
问题描述
我可以使用Javascript客户端从Google Cloud Endpoints获取博客文章列表:
gapi.client.blog。 posts.list()。execute(function(resp){
console.log(resp);
});
但我需要在包含用户令牌的Google Cloud Endpoints请求中设置自定义标头值(这可能是来自Facebook的访问令牌)。我该如何使用Google的Javascript客户端来做到这一点?我可以通过不使用Google的Javascript客户端来解决此问题,但我宁愿使用它。
https://developers.google.com/appengine/docs/java/endpoints/consume_js
https://developers.google.com/api-client-library/javascript/reference/referencedocs
编辑
看来我可以像这样传递自定义标头值:
gapi.auth.setToken({
access_token:'这是我的自定义值'
});
虽然看起来不太好。有没有更好的方法来做到这一点?
现在可以使用 gapi.client.request ,例如: gapi.client.init({$ b $'clientId':'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
'scope':'your_scope'
})。then(function(){
return gapi.client.request({$ b $'path':'http:// path / to / your / endpoints / api',
'headers':{'mycustomheader':'myvalue'}
})
})。then(function(response){
console.log(response.result);
$ b console.log('Error:'+ reason.result.error.message);
});
另请参阅入门指南页面的Google API Javascript客户端文档。
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?
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.
这篇关于如何在Google Cloud Endpoints Javascript客户端中设置自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!