Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently making API calls to my backend using the Google Cloud Endpoint generated JavaScript Client. The problem is the cookies for my page are not being added to the HTTP requests. How can I add the Gitkit gtoken cookie to my request.

  • Backend is Google App Engine Java
  • Using Goole Cloud Endpoints to build my API
  • Using the Google Cloud Endpoints JavaScript web client loaded as follows gapi.client.load('myApi', 'v1', resourceLoaded, 'https://my-project-id.appspot.com/_ah/api');

I have already configured Google Cloud Endpoints, on the backend, to allow cookies. auth = @ApiAuth(allowCookieAuth = AnnotationBoolean.TRUE)

My endpoint looks as follows.

@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!
     */

    ...

}

I am making calls with the Google Cloud Endpoints JS client as so.

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

Is there something I have to do to make sure the Endpoints JS client includes the gtoken cookie in it's request?

like image 723
Marc M. Avatar asked Apr 26 '16 21:04

Marc M.


1 Answers

You better add screenshots of cookies storage + request headers and create a plunker/jsfiddle/jsbin to reproduce the problem.

There are chances that cookies are not set or not send to server. You need to localize where is a problem. If it's sent over wire by browser then issue is on server side. If it's in cookies storage but not sent it's client issue. If it's not in storage there is just nothing to sent and it's a different problem to find out why they are not at client at all.

You can view cookies & requests headers in devtools of your browser. And yes, cookies are send automatically if not expired and match to host & path prefix.

like image 98
Alexander Trakhimenok Avatar answered Nov 14 '22 12:11

Alexander Trakhimenok