7

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?

Marc M.
  • 3,631
  • 4
  • 32
  • 53
  • Open bounty and still no answers! Is there something I can do to make this question more clear? – Marc M. Apr 29 '16 at 02:45
  • 1
    Hi Marc, you can add screenshots of cookies storage + request headers and create a plunker/jsfiddle/jsbin to reproduce the problem. – Alexander Trakhimenok Apr 29 '16 at 12:15
  • @AlexanderTrakhimenok Do you mean cookies storage on the browser side? – Marc M. Apr 29 '16 at 20:43
  • Yes, 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. – Alexander Trakhimenok Apr 29 '16 at 22:22
  • @AlexanderTrakhimenok Okay, Thanks. Forgive my novices abilities, I am new to cookies. Where is the cookie storage? Are you just asking for a print out of `document.cookie`? – Marc M. Apr 29 '16 at 22:37
  • @AlexanderTrakhimenok Also, it sounds like what you are saying is cookies should be packaged with request automatically if everything is in order. Is that correct? – Marc M. Apr 29 '16 at 22:42
  • 1
    You can view cookies & requests headers in dev tools of your browser. And yes, cookies are send automatically if not expired and match to host & path prefix. – Alexander Trakhimenok Apr 30 '16 at 00:19
  • @AlexanderTrakhimenok Solved my problem. The problem was that I was making calls to my API, hosted on a remote GAE server, from my http://localhost:8080/ dev server. I thought that any cookie for that page would be included in all request to any server. I did not understand that the request destination needed to match the cookie's domain. – Marc M. Apr 30 '16 at 00:45
  • @AlexanderTrakhimenok If you post an answer I can give you the bounty. I feel like an idiot. – Marc M. Apr 30 '16 at 00:48
  • Ok, I will a bit later - would be my first bounty :) – Alexander Trakhimenok Apr 30 '16 at 11:17
  • Posted summary as an answer. – Alexander Trakhimenok May 03 '16 at 12:55

1 Answers1

2

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.

Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
  • Solved my problem. The problem was that I was making calls to my API, hosted on a remote GAE server, from my localhost:8080 dev server. I thought that any cookie for that page would be included in all request to any server. I did not understand that the request destination needed to match the cookie's domain. – Marc M. May 05 '16 at 21:31
  • Hi Marc, could you let me know the way to solve this problem ? – Chameron Nov 18 '17 at 01:43