使用Google Cloud Endpoints JavaScript客户端时,在HTTP请求中包含Cookie

Mar*_* M. 7 cookies google-app-engine google-cloud-endpoints google-identity-toolkit

我目前正在使用Google Cloud Endpoint生成的JavaScript客户端向我的后端发出API调用.问题是我的页面的cookie没有被添加到HTTP请求中.如何将Gitkit gtoken cookie添加到我的请求中.

  • 后端是Google App Engine Java
  • 使用Goole Cloud Endpoints构建我的API
  • 使用Google Cloud Endpoints JavaScript Web客户端加载如下 gapi.client.load('myApi', 'v1', resourceLoaded, 'https://my-project-id.appspot.com/_ah/api');

我已经在后端配置了Google Cloud Endpoints以允许Cookie. auth = @ApiAuth(allowCookieAuth = AnnotationBoolean.TRUE)

我的端点如下所示.

@ApiMethod(path = "user-account")
public UserAccount get(HttpServletRequest httpRequest) {

    GitkitUser gitkitUser = Gitkit.validate(httpRequest); // returns null

    Cookie[] cookies = httpRequest.getCookies();
    log.severe("# of cookies: " + cookies.length);
    for (Cookie cookie : cookies) {
       log.severe("cookie name: " + cookie.getName());
        log.severe("cookie value: " + cookie.getValue()); 
    }

    /*
     * Logs 1 for # of cookies, with a cookie name of "G_ENABLED_IDPS" 
     * a value of "google". No gtoken cookie, even though I have 
     * checked and there is one!
     */

    ...

}
Run Code Online (Sandbox Code Playgroud)

我正在与Google Cloud Endpoints JS客户端进行通话.

gapi.client.myApi.userAccountResource.get().execute(function (resp){
    ...
});
Run Code Online (Sandbox Code Playgroud)

我有什么必须做的,以确保Endpoints JS客户端在其请求中包含gtoken cookie吗?

Ale*_*nok 2

您最好添加 cookie 存储 + 请求标头的屏幕截图并创建 plunker/jsfiddle/jsbin 来重现问题。

Cookie 可能未设置或未发送到服务器。您需要定位问题所在。如果它是通过浏览器通过网络发送的,则问题出在服务器端。如果它在 cookie 存储中但未发送,则属于客户端问题。如果它不在存储中,则没有任何内容可发送,并且找出它们根本不在客户端的原因是一个不同的问题。

您可以在浏览器的开发工具中查看 cookie 和请求标头。是的,如果未过期并且与主机和路径前缀匹配,cookie 会自动发送。