如何在Google Cloud Endpoints Javascript客户端中设置自定义标题?

编程入门 行业动态 更新时间:2024-10-14 16:21:46
本文介绍了如何在Google Cloud Endpoints Javascript客户端中设置自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以使用Javascript客户端从Google Cloud Endpoints获取博客文章列表:

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

但我需要在包含用户令牌的Google Cloud Endpoints请求中设置自定义标头值(这可能是来自Facebook的访问令牌)。我该如何使用Google的Javascript客户端来做到这一点?我可以通过不使用Google的Javascript客户端来解决此问题,但我宁愿使用它。

developers.google/appengine/docs/java/endpoints/consume_js developers.google/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','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.

developers.google/appengine/docs/java/endpoints/consume_js developers.google/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', 'scope': 'your_scope' }).then(function() { return gapi.client.request({ 'path': '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客户端中设置自定义标题?

本文发布于:2023-11-25 23:37:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   客户端   标题   如何在   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!