How to use session tokens correctly?

夙愿已清 提交于 2020-07-09 12:10:34

问题


I am using the Java client for the Google Places API:

<dependency>
    <groupId>com.google.maps</groupId>
    <artifactId>google-maps-services</artifactId>
    <version>0.9.1</version>
</dependency>

The following is how I use the autocomplete API on my server:

public List<AutocompletePrediction> searchAddress(String query) {

    List<AutocompletePrediction> predictions = new ArrayList<>();

    try {
        AutocompletePrediction[] autocompletePredictions = PlacesApi
                .placeAutocomplete(geoApiContext, query, null) 

        predictions.addAll(Arrays.asList(autocompletePredictions));
    } catch (ApiException | InterruptedException | IOException e) {
        e.printStackTrace();
    }

    return predictions;
}

Now, at the time I am passing null as the sessionToken:

.placeAutocomplete(geoApiContext, query, null) 

mainly because I am not entirely sure how these are supposed to work.

I could create one token every 2 minutes and use that token no matter which user is currently typing.

So this would mean if two users search for "location" and "place", the incoming queries might look like this:

[User 1] 1. lo
[User 2] 2. p
[User 1] 3. loca
[User 2] 4. plac
[User 1] 5. locat
[User 1] 6. location
[User 2] 7. place

now, I could use for all those requests the same token X and create a new one every one or two minutes but I don't know whether this is allowed and whether this affects billing.

The other way would be to create a cache per user which stores for user 1 and 2 a token X1 and X2 respectively. This solution would be a little more complex which is why I'd like to know whether the first solution would already work in the sense of I won't get billed for every single request.

Documentation

  • Session Tokens
  • Autocomplete Sesssions

回答1:


You should be using session tokens because they do affect billing. According to Google's documentation:

If the sessiontoken parameter is omitted, or if you reuse a session token, the session is charged as if no session token was provided.

Example of request that uses a sessiontoken:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=1600+Amphitheatre&key=<API_KEY>&sessiontoken=1234567890

Please create a new and unique session token for each Autocomplete session as suggested in the linked documentation.

To further clarify on sessions:

A session token is good for one user session and should not be used for more than one user session.

A session begins when the user starts typing a query, and concludes when they select a place (i.e. when a Place Details call is made). This is billed on the Autocomplete (included with Places Details) – Per Session SKU.

If this one user does not make a selection, the session will end after a short time out period (i.e. within a few minutes of the beginning of the session). This is billed on the Autocomplete without Places Details – Per Session SKU.

If you do not use a session token or it is deemed invalid, you will be billed on the Autocomplete – Per Request SKU.

Hope this helps.



来源:https://stackoverflow.com/questions/57340818/how-to-use-session-tokens-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!