GCP

【Google Maps Platform】This API project is not authorized to use this API の対応方法

開発環境

  • Python3
  • Jupyter Notebook
  • Docker

概要

PythonでGoogle Maps PlatformのAPIを使って、位置情報から周りのMap情報を取得しようとした際に、

This API project is not authorized to use this API というエラーで引っかかってしまいました。

places_result = gmaps.places_nearby(location=location, radius=radius, type='restaurant')
ApiError                                  Traceback (most recent call last)
Cell In[12], line 30
     27 latitude = 35.6895
     28 longitude = 139.6917
---> 30 restaurants_df = fetch_restaurant_data(api_key, latitude, longitude)
     31 print(restaurants_df)
     32 # restaurants_df.to_excel("restaurants_nearby.xlsx")

Cell In[12], line 11, in fetch_restaurant_data(api_key, latitude, longitude)
      8 location = (latitude, longitude)
      9 radius = 10  # 1キロメートル
---> 11 places_result = gmaps.places_nearby(location=location, radius=radius, type='restaurant')
     12 # restaurants = []
     13 
     14 # for place in places_result['results']:
   (...)
     21 
     22 # return pd.DataFrame(restaurants)
     23 return 123

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/googlemaps/client.py:445, in make_api_method.<locals>.wrapper(*args, **kwargs)
    442 @functools.wraps(func)
    443 def wrapper(*args, **kwargs):
    444     args[0]._extra_params = kwargs.pop("extra_params", None)
--> 445     result = func(*args, **kwargs)
    446     try:
    447         del args[0]._extra_params

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/googlemaps/places.py:360, in places_nearby(client, location, radius, keyword, language, min_price, max_price, name, open_now, rank_by, type, page_token)
    355     elif radius is not None:
    356         raise ValueError(
    357             "radius cannot be specified when rank_by is set to " "distance"
    358         )
--> 360 return _places(
    361     client,
    362     "nearby",
    363     location=location,
    364     radius=radius,
    365     keyword=keyword,
    366     language=language,
    367     min_price=min_price,
    368     max_price=max_price,
    369     name=name,
    370     open_now=open_now,
    371     rank_by=rank_by,
    372     type=type,
    373     page_token=page_token,
    374 )

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/googlemaps/places.py:425, in _places(client, url_part, query, location, radius, keyword, language, min_price, max_price, name, open_now, rank_by, type, region, page_token)
    422     params["pagetoken"] = page_token
    424 url = "/maps/api/place/%ssearch/json" % url_part
--> 425 return client._request(url, params)

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/googlemaps/client.py:340, in Client._request(self, url, params, first_request_time, retry_counter, base_url, accepts_clientid, extract_body, requests_kwargs, post_json)
    338     result = extract_body(response)
    339 else:
--> 340     result = self._get_body(response)
    341 self.sent_times.append(time.time())
    342 return result

File /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/googlemaps/client.py:369, in Client._get_body(self, response)
    365 if api_status == "OVER_QUERY_LIMIT":
    366     raise googlemaps.exceptions._OverQueryLimit(
    367         api_status, body.get("error_message"))
--> 369 raise googlemaps.exceptions.ApiError(api_status,
    370                                      body.get("error_message"))

ApiError: REQUEST_DENIED (This API project is not authorized to use this API.)

APIキーは作成できているが、このエラーが出てしまった方はこの記事のポイントを確認してみてください。

Error: This API project is not authorized to use this API の対応方法

結論。

Google Maps PlatformのAPIを有効にする

Google CloudのAPIサービスでGoogle Maps Platformを有効にして、APIも有効する必要があります。

「APIを有効にする」で「APIの有効化」をクリックしないとGoogle Maps PlatformのAPIは使えません。

APIキーでGeocoding APIとPlaces APIを有効化する

APIキーで適切なAPIを選択する必要があります。

gmaps.places_nearby()位置情報から周囲の情報を取得したい方は下記2つのAPIが必須です。

  • Geocoding API
  • Places API

-GCP
-