Google地方AutoComplete小部件正在为每个请求生成新的会话密钥 [英] Google places AutoComplete widget is generating a new Session Key per every request

本文介绍了Google地方AutoComplete小部件正在为每个请求生成新的会话密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,Google更新了一些结算政策,作为他们为自动填充请求引入了SessionTokens的一部分。



作为其中的一部分,每个请求都是相同的如果我们使用Google API提供的自动完成小部件,会话将在请求中以相同的标记发送,并且它是自动的。



但是,我看到生成了不同的令牌同一会话中的每个请求。我错过了什么,请指导我。以下是样本的










< a href =https://i.stack.imgur.com/WxTGO.png =noreferrer>


Recently, Google has updated some billing policies as part of that they've introduced SessionTokens for the Autocomplete requests.

As part of that, every request in the same session will be sent the same token in the request and it's automatic if we use the AutoComplete widgets provided by Google APIs.

But, I'm seeing a different token generated for every request in the same session. I'm I missing anything, please guide me. Here is the pluker for the sample, there I observed a different token generated for each keystroke.

var options = {
  types: ['(cities)']
};
  var searchBox = new google.maps.places.Autocomplete(input, options);

解决方案

My Code for the page

```

<!-- footer -->
<script>
// initiate search area for autocomplete of places from Google Maps Street Addresses
function initAutocomplete() {
    // define the search location box
    var searchBox = $("#searchLocation")[0];

    // initiate the autocomplete with the options on the search box
    var autocomplete = new google.maps.places.Autocomplete(searchBox, options);



};
</script>

<script src="https://maps.googleapis.com/maps/api/js?libraries=places&region=ca&language=en&key= [YOUR_API_KEY]&callback=initAutocomplete" async defer> 

```

Session Token Clarification

According to the Google Docs (all the way at the bottom of the page in a "warning text")... https://developers.google.com/maps/documentation/javascript/places-autocomplete

Warning: Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually. Note that the Autocomplete Widget handles sessions automatically (you don't need to do anything additional). Now, lets dive into the basic Javascript example.

This may be a bit confusing, only because you are trying to get into the nomenclature of the google docs and what these things and possibly because you were doing it one way and suddenly your boss or client or someone else ask you to use Sessions/Session Tokens instead. If you do new google.maps.places.Autocomplete() instead of AutocompleteServices or something along those lines, you are using the "Autocomplete Widget". The autocomplete widget will handle the session / session tokens itself.

Prove it!

I trust google and google docs, but there was plenty of confusion on the web about this. So I wanted proof, besides getting a bill after a month. FYI - the billing in Google Clound Platform Console (https://console.cloud.google.com/google/maps-apis?pli=1) will show you this almost right away and will corroborate the stuff below and above.

Looking at the Network activity in Dev Tools, we see that there is a few calls to the Autocomplete Service. I did not use the "AutocompleteService" function here, this is a basic example setup the same way I have listed above for the Callback with using the "Autocomplete Widget" via new google.maps.places.Autocomplete().

When you go to the page, you can see the call to library first, first screenshot of Dev Tools below. Next I did a search, which worked. I started to type in a basic address of 1990, you can see 4 request made to the API after that. 1 request/call for each character I type in. Each of these request names begin with "AutocompleteServices.GetPredictions...", even though I DID NOT USE AutocompleteServices in my code. On the back-end, the "Autocomplete Widget" is using the "AutocompeleteServices" and doing all the UI/UX work for you as well as the functionality and the session and tokens. These are in the Dev tools screenshots 1-3 below. Then you see a 5th request call to "PlaceServces.GetPlaceDetails", which was when I selected from the dropdown. This is the dev tools screenshot 4 below. Looking at the headers, for each of these requests, I see a few things. The first thing, circled at the bottom is a "token". I know I just said and Google Docs just said that the "Autocomplete Widget" handles the session token, and a keen eye will see that this "token" value is different for every request. It is also not in 'version 4 UUID' format, which is recommended by Google, but that is a different topic. This "token" in the header, IS NOT the session token. This is buried in documentation that is 5 years old for a previous version of this on Google Docs, but this is not used anymore, and is auto-set for each request and is not the Session token. SO, "where is the session token then?" you might ask.
The other items in the header that start with 1s, 2s and so on are different session variables that are passed. In this case, the session token is '20s' followed by the version 4 UUID auto created by the "Autocomplete Widget". A keen eye will also notice that for the 3 screenshots below they are the same. I have a fifth screenshot after the page refreshes that shows that the Session Token has changed. You can see in my code that I did not specify those things, but because I am using the "Autocomplete Widget" they are done for me.

这篇关于Google地方AutoComplete小部件正在为每个请求生成新的会话密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆