API project is not authorized to use this even no ristriction

I have a issue regarding
Directions API Response: { “error_message” : “This API project is not authorized to use this API.”, “routes” : [], “status” : “REQUEST_DENIED”}

this is error i always received from my Direction API whenever i tried calling to create rout between the origin and destination

for testing i used Log in android studio
(No routes found in the response.)
but this authorization issue always occurs.

I’m am using same API in Map SDK JavaScript & also using into Map SDK Android.
even while loading map on the android that key works. location markers update perfectly,
on Map SDK JavaScript with same api the routes working.
still the problem remains same in android studio.
in Android studio it places destination marker successfully but no route while creating the route.

Cross checked everything.
Enabling all api key
No ristriction
Generated new key’s multiple times.
even created new project to generate new api. will anyone help me out.

Thanks & Regards
Stuck from last one Week.


This code for DirectionApi.java
public class DirectionsApi {

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static String getDirectionsUrl(LatLng origin, LatLng destination, String apiKey) {
String url = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=" + origin.latitude + "," + origin.longitude +
"&destination=" + destination.latitude + "," + destination.longitude +
"&mode=driving&key=" + apiKey;
System.out.println("Directions API URL: " + url);
return url;
}
public static String requestDirections(String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response: " + response.toString());
return response.toString();
} else {
return null;
}
}
public static List<LatLng> parseDirectionsResponse(String response) throws JSONException {
JSONObject jsonObject = new JSONObject(response);
JSONArray routes = jsonObject.getJSONArray("routes");
if (routes.length() == 0) {
return null;
}
JSONObject route = routes.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject leg = legs.getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");
List<LatLng> routeCoordinates = new ArrayList<>();
for (int i = 0; i < steps.length(); i++) {
JSONObject step = steps.getJSONObject(i);
JSONObject polyline = step.getJSONObject("polyline");
String polylineString = polyline.getString("points");
List<LatLng> polylineCoordinates = decodePolyline(polylineString);
routeCoordinates.addAll(polylineCoordinates);
}
return routeCoordinates;
}
private static List<LatLng> decodePolyline(String polylineString) {
List<LatLng> polylineCoordinates = new ArrayList<>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < polylineString.length()) {
int b;
int shift = 0;
int result = 0;
do {
b = polylineString.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = polylineString.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
polylineCoordinates.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return polylineCoordinates;
}
</code>
<code>public static String getDirectionsUrl(LatLng origin, LatLng destination, String apiKey) { String url = "https://maps.googleapis.com/maps/api/directions/json?" + "origin=" + origin.latitude + "," + origin.longitude + "&destination=" + destination.latitude + "," + destination.longitude + "&mode=driving&key=" + apiKey; System.out.println("Directions API URL: " + url); return url; } public static String requestDirections(String url) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); System.out.println("Response Code: " + responseCode); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println("Response: " + response.toString()); return response.toString(); } else { return null; } } public static List<LatLng> parseDirectionsResponse(String response) throws JSONException { JSONObject jsonObject = new JSONObject(response); JSONArray routes = jsonObject.getJSONArray("routes"); if (routes.length() == 0) { return null; } JSONObject route = routes.getJSONObject(0); JSONArray legs = route.getJSONArray("legs"); JSONObject leg = legs.getJSONObject(0); JSONArray steps = leg.getJSONArray("steps"); List<LatLng> routeCoordinates = new ArrayList<>(); for (int i = 0; i < steps.length(); i++) { JSONObject step = steps.getJSONObject(i); JSONObject polyline = step.getJSONObject("polyline"); String polylineString = polyline.getString("points"); List<LatLng> polylineCoordinates = decodePolyline(polylineString); routeCoordinates.addAll(polylineCoordinates); } return routeCoordinates; } private static List<LatLng> decodePolyline(String polylineString) { List<LatLng> polylineCoordinates = new ArrayList<>(); int index = 0; int lat = 0; int lng = 0; while (index < polylineString.length()) { int b; int shift = 0; int result = 0; do { b = polylineString.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = polylineString.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; polylineCoordinates.add(new LatLng(lat * 1e-5, lng * 1e-5)); } return polylineCoordinates; } </code>
public static String getDirectionsUrl(LatLng origin, LatLng destination, String apiKey) {
    String url = "https://maps.googleapis.com/maps/api/directions/json?" +
            "origin=" + origin.latitude + "," + origin.longitude +
            "&destination=" + destination.latitude + "," + destination.longitude +
            "&mode=driving&key=" + apiKey;
    System.out.println("Directions API URL: " + url);
    return url;
}

public static String requestDirections(String url) throws IOException {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");

    int responseCode = con.getResponseCode();
    System.out.println("Response Code: " + responseCode);
    if (responseCode == 200) {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println("Response: " + response.toString());
        return response.toString();
    } else {
        return null;
    }
}

public static List<LatLng> parseDirectionsResponse(String response) throws JSONException {
    JSONObject jsonObject = new JSONObject(response);
    JSONArray routes = jsonObject.getJSONArray("routes");
    if (routes.length() == 0) {
        return null;
    }
    JSONObject route = routes.getJSONObject(0);
    JSONArray legs = route.getJSONArray("legs");
    JSONObject leg = legs.getJSONObject(0);
    JSONArray steps = leg.getJSONArray("steps");

    List<LatLng> routeCoordinates = new ArrayList<>();
    for (int i = 0; i < steps.length(); i++) {
        JSONObject step = steps.getJSONObject(i);
        JSONObject polyline = step.getJSONObject("polyline");
        String polylineString = polyline.getString("points");
        List<LatLng> polylineCoordinates = decodePolyline(polylineString);
        routeCoordinates.addAll(polylineCoordinates);
    }

    return routeCoordinates;
}

private static List<LatLng> decodePolyline(String polylineString) {
    List<LatLng> polylineCoordinates = new ArrayList<>();
    int index = 0;
    int lat = 0;
    int lng = 0;

    while (index < polylineString.length()) {
        int b;
        int shift = 0;
        int result = 0;

        do {
            b = polylineString.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;

        do {
            b = polylineString.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        polylineCoordinates.add(new LatLng(lat * 1e-5, lng * 1e-5));
    }

    return polylineCoordinates;
}

}


This is code of MapsActivity.java

private void getDirectionsToUser(LatLng userLatLng) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
return;
}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> // Show the progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Fetching route...");
progressDialog.setCancelable(false);
progressDialog.show();
fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
if (location != null) {
serviceProviderLatLng = new LatLng(location.getLatitude(), location.getLongitude());
String apiKey = getString(R.string.google_api_key); // Use the correct API key
String url = DirectionsApi.getDirectionsUrl(serviceProviderLatLng, userLatLng, apiKey);
executorService.execute(() -> {
try {
String result = DirectionsApi.requestDirections(url);
runOnUiThread(() -> {
// Dismiss the progress dialog
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (result != null) {
System.out.println("Directions API Response: " + result);
try {
List<LatLng> routeCoordinates = DirectionsApi.parseDirectionsResponse(result);
if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
addPolylinesToMap(routeCoordinates);
} else {
Log.e("MapsActivity", "No routes found in the response.");
Toast.makeText(MapsActivity.this, "No routes found.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.e(TAG, "Failed to parse directions", e);
Toast.makeText(MapsActivity.this, "Failed to parse directions.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
Log.e(TAG, "Failed to fetch directions", e);
runOnUiThread(() -> {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
});
}
});
}
});
}
private void addPolylinesToMap(final List<LatLng> routeCoordinates) {
// Use a handler to run the code on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(routeCoordinates));
polyline.setColor(ContextCompat.getColor(MapsActivity.this, R.color.blue));
polyline.setClickable(true);
} else {
Log.w(TAG, "run: No routes found.");
}
});
}
</code>
<code> // Show the progress dialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Fetching route..."); progressDialog.setCancelable(false); progressDialog.show(); fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> { if (location != null) { serviceProviderLatLng = new LatLng(location.getLatitude(), location.getLongitude()); String apiKey = getString(R.string.google_api_key); // Use the correct API key String url = DirectionsApi.getDirectionsUrl(serviceProviderLatLng, userLatLng, apiKey); executorService.execute(() -> { try { String result = DirectionsApi.requestDirections(url); runOnUiThread(() -> { // Dismiss the progress dialog if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } if (result != null) { System.out.println("Directions API Response: " + result); try { List<LatLng> routeCoordinates = DirectionsApi.parseDirectionsResponse(result); if (routeCoordinates != null && !routeCoordinates.isEmpty()) { addPolylinesToMap(routeCoordinates); } else { Log.e("MapsActivity", "No routes found in the response."); Toast.makeText(MapsActivity.this, "No routes found.", Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { Log.e(TAG, "Failed to parse directions", e); Toast.makeText(MapsActivity.this, "Failed to parse directions.", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show(); } }); } catch (IOException e) { Log.e(TAG, "Failed to fetch directions", e); runOnUiThread(() -> { if (progressDialog != null && progressDialog.isShowing()) { progressDialog.dismiss(); } Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show(); }); } }); } }); } private void addPolylinesToMap(final List<LatLng> routeCoordinates) { // Use a handler to run the code on the main thread new Handler(Looper.getMainLooper()).post(() -> { if (routeCoordinates != null && !routeCoordinates.isEmpty()) { Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(routeCoordinates)); polyline.setColor(ContextCompat.getColor(MapsActivity.this, R.color.blue)); polyline.setClickable(true); } else { Log.w(TAG, "run: No routes found."); } }); } </code>
    // Show the progress dialog
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Fetching route...");
    progressDialog.setCancelable(false);
    progressDialog.show();

    fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
        if (location != null) {
            serviceProviderLatLng = new LatLng(location.getLatitude(), location.getLongitude());
            String apiKey = getString(R.string.google_api_key); // Use the correct API key
            String url = DirectionsApi.getDirectionsUrl(serviceProviderLatLng, userLatLng, apiKey);

            executorService.execute(() -> {
                try {
                    String result = DirectionsApi.requestDirections(url);
                    runOnUiThread(() -> {
                        // Dismiss the progress dialog
                        if (progressDialog != null && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }

                        if (result != null) {
                            System.out.println("Directions API Response: " + result);
                            try {
                                List<LatLng> routeCoordinates = DirectionsApi.parseDirectionsResponse(result);
                                if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
                                    addPolylinesToMap(routeCoordinates);
                                } else {
                                    Log.e("MapsActivity", "No routes found in the response.");
                                    Toast.makeText(MapsActivity.this, "No routes found.", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                Log.e(TAG, "Failed to parse directions", e);
                                Toast.makeText(MapsActivity.this, "Failed to parse directions.", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (IOException e) {
                    Log.e(TAG, "Failed to fetch directions", e);
                    runOnUiThread(() -> {
                        if (progressDialog != null && progressDialog.isShowing()) {
                            progressDialog.dismiss();
                        }
                        Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
                    });
                }
            });
        }
    });
}

private void addPolylinesToMap(final List<LatLng> routeCoordinates) {
    // Use a handler to run the code on the main thread
    new Handler(Looper.getMainLooper()).post(() -> {
        if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
            Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(routeCoordinates));
            polyline.setColor(ContextCompat.getColor(MapsActivity.this, R.color.blue));
            polyline.setClickable(true);
        } else {
            Log.w(TAG, "run: No routes found.");
        }
    });
}

I want to create route between Origin and destination
Directions API Response: { “error_message” : “This API project is not authorized to use this API.”, “routes” : [], “status” : “REQUEST_DENIED”}

New contributor

Gautam chibi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật