最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Google Maps API fails "This API project is not authorized to use this API." - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a simple call to Maps API to get direction between two addresses. Have reviewed all the answers here and tried the suggestions provided, but none works. To summarize, when I access the following in a browser it succeeds. But in code that uses the new Routes API I get the above error message.

This works, .0000,133.0000&key=<API_KEY>

The below code does not work,

!pip install googlemaps
import googlemaps

def get_optimal_route(origin, destination, waypoints):

    gmaps = googlemaps.Client(key='<API_KEY>')
    try:
        directions_result = gmaps.directions(
            origin,
            destination,
            waypoints=waypoints,
            optimize_waypoints=True,
            mode="driving"
        )

        if directions_result:
            route = directions_result[0]
            ordered_waypoints = [waypoints[i] for i in route['waypoint_order']]
            total_distance = sum(leg['distance']['value'] for leg in route['legs'])
            total_duration = sum(leg['duration']['value'] for leg in route['legs'])

            return {
                "optimized_waypoints": ordered_waypoints,
                "total_distance_meters": total_distance,
                "total_duration_seconds": total_duration,
                "full_route_details": route
            }
        else:
            return {"error": "No route found"}
    except Exception as e:
        return {"error": str(e)}

origin = "1600 Amphitheatre Parkway, Mountain View, CA"
destination = "48 Wall Street, New York, NY"
waypoints = None

optimal_route = get_optimal_route(origin, destination, waypoints)

if "error" not in optimal_route:
    print("Optimal Route:")
    print("Ordered Waypoints:", optimal_route["optimized_waypoints"])
    print("Total Distance:", optimal_route["total_distance_meters"], "meters")
    print("Total Duration:", optimal_route["total_duration_seconds"], "seconds")
    #print("Full Route Details:", optimal_route["full_route_details"]) # Print full details if needed
else:
    print("Error:", optimal_route["error"]) 

I have tried the following,

  1. Checked that all the required APIs are enabled. I have even removed all API restrictions.
  2. Removed Application restriction.
  3. Recycled my API Key
  4. Shutdown and restarted my App
  5. I saw recommendations to enable Direction API, but this has been phased out starting March 1, 2025

Anything else I should try?

I am trying to make a simple call to Maps API to get direction between two addresses. Have reviewed all the answers here and tried the suggestions provided, but none works. To summarize, when I access the following in a browser it succeeds. But in code that uses the new Routes API I get the above error message.

This works, https://maps.googleapis/maps/api/geocode/json?latlng=-27.0000,133.0000&key=<API_KEY>

The below code does not work,

!pip install googlemaps
import googlemaps

def get_optimal_route(origin, destination, waypoints):

    gmaps = googlemaps.Client(key='<API_KEY>')
    try:
        directions_result = gmaps.directions(
            origin,
            destination,
            waypoints=waypoints,
            optimize_waypoints=True,
            mode="driving"
        )

        if directions_result:
            route = directions_result[0]
            ordered_waypoints = [waypoints[i] for i in route['waypoint_order']]
            total_distance = sum(leg['distance']['value'] for leg in route['legs'])
            total_duration = sum(leg['duration']['value'] for leg in route['legs'])

            return {
                "optimized_waypoints": ordered_waypoints,
                "total_distance_meters": total_distance,
                "total_duration_seconds": total_duration,
                "full_route_details": route
            }
        else:
            return {"error": "No route found"}
    except Exception as e:
        return {"error": str(e)}

origin = "1600 Amphitheatre Parkway, Mountain View, CA"
destination = "48 Wall Street, New York, NY"
waypoints = None

optimal_route = get_optimal_route(origin, destination, waypoints)

if "error" not in optimal_route:
    print("Optimal Route:")
    print("Ordered Waypoints:", optimal_route["optimized_waypoints"])
    print("Total Distance:", optimal_route["total_distance_meters"], "meters")
    print("Total Duration:", optimal_route["total_duration_seconds"], "seconds")
    #print("Full Route Details:", optimal_route["full_route_details"]) # Print full details if needed
else:
    print("Error:", optimal_route["error"]) 

I have tried the following,

  1. Checked that all the required APIs are enabled. I have even removed all API restrictions.
  2. Removed Application restriction.
  3. Recycled my API Key
  4. Shutdown and restarted my App
  5. I saw recommendations to enable Direction API, but this has been phased out starting March 1, 2025

Anything else I should try?

Share edited Mar 14 at 18:22 user2517962 asked Mar 14 at 14:05 user2517962user2517962 295 bronze badges 3
  • Are you URL encoding the strings in the URL? – geocodezip Commented Mar 14 at 17:01
  • I am directly calling the Directions API. I assumed it will do the required URL encoding. – user2517962 Commented Mar 14 at 18:20
  • 1 I found the solution. Even though Google says that Direction API is deprecated and it does not show up when you search for it in your Google Cloud project, this API needs to be enabled. I found a link to the Direction API in the Google Maps Documentation. Once enabled my code worked. Below is the link to the documentation page. developers.google/maps/documentation/javascript/directions – user2517962 Commented Mar 15 at 2:35
Add a comment  | 

1 Answer 1

Reset to default 0

You can solve this by enabling the Direction API. Currently, the only way to do this is by going to this link (because its in Legacy).

发布评论

评论列表(0)

  1. 暂无评论