如何正确使用会话令牌? [英] How to use session tokens correctly?
问题描述
我将Java客户端用于Google Places API:
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>
以下是我如何在服务器上使用自动完成API:
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;
}
现在,在我将null
作为sessionToken
传递时:
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.
我可以每2分钟创建一个一个令牌,无论当前正在输入哪个用户,都可以使用该令牌.
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
现在,我可以为所有这些请求使用相同的令牌X
,每隔一到两分钟创建一个新令牌,但是我不知道是否允许这样做以及这是否会影响结算.
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.
另一种方法是为每个用户创建一个缓存,该缓存分别为用户1
和2
存储令牌X1
和X2
.这种解决方案会稍微复杂一点,这就是为什么我想知道第一种解决方案是否已经可以正常工作,因为我不会为每个请求付费.
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.
- Session Tokens
- Autocomplete Sesssions
推荐答案
您应该使用会话令牌,因为它们确实会影响计费.根据 Google的文档:
You should be using session tokens because they do affect billing. According to Google's documentation:
如果省略了
sessiontoken
参数,或者您重用了会话 令牌,则对会话进行收费,就像没有提供会话令牌一样.
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.
会话在用户开始输入查询时开始,并在他们选择位置时结束(例如,当地点详情的调用).这是通过自动完成(包含在地方详情"中)计费的–每个会话的SKU .
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.
如果此用户未选择 ,则会话将在短暂的超时时间(即会话开始后的几分钟内)之后结束.这是在没有地点详细信息的自动完成–每个会话中计费的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.
如果您不使用会话令牌或被视为无效,则会在自动完成-根据请求的SKU .
If you do not use a session token or it is deemed invalid, you will be billed on the Autocomplete – Per Request SKU.
希望这会有所帮助.
这篇关于如何正确使用会话令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!