簡體   English   中英

使用Google Cloud Endpoints JavaScript客戶端時,在HTTP請求中包含Cookie

[英]Include cookies in HTTP requests when using the Google Cloud Endpoints JavaScript client

我目前正在使用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!
     */

    ...

}

我正在與Google Cloud Endpoints JS客戶端進行通話。

gapi.client.myApi.userAccountResource.get().execute(function (resp){
    ...
});

我有什么必須做的,以確保Endpoints JS客戶端在其請求中包含gtoken cookie嗎?

您最好添加cookie存儲+請求標頭的屏幕截圖,並創建一個plunker / jsfiddle / jsbin來重現問題。

有可能沒有設置cookie或不發送到服務器。 您需要本地化問題所在。 如果它是通過瀏覽器通過線路發送的,那么問題是在服務器端。 如果它存在於cookie存儲中但未發送它的客戶端問題。 如果它沒有存儲,那么就沒有什么可以發送的,而且找出它們根本不在客戶端的原因是另一個問題。

您可以在瀏覽器的devtools中查看Cookie和請求標頭。 是的,如果沒有過期,cookie會自動發送,並與主機和路徑前綴匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM