简体   繁体   English

为什么我不能使用新创建的API密钥?

[英]Why can't I use my newly created API key?

I had just now created a new API key after enabling Places API in the developer console. 我刚刚在开发人员控制台中启用Places API后,我刚刚创建了一个新的API密钥。

Code for implementing Place picker API: 实施Place picker API的代码:

 Places.initialize(getApplicationContext(), "@string/google_place_api");
    List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
                List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
                RectangularBounds bounds = RectangularBounds.newInstance(
                        new LatLng(-33.880490, 151.184363),
                        new LatLng(-33.858754, 151.229596));
                Intent autocompleteIntent =
                        new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
                                .setLocationBias(bounds)
                                .setTypeFilter(typeFilters.get(0))
                                .build(getApplicationContext());
                startActivityForResult(autocompleteIntent, 1001);




@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                Toast.makeText(getApplicationContext(),place.getName()+ ", " + place.getId(), Toast.LENGTH_SHORT).show();
            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Toast.makeText(getApplicationContext(), status.getStatusMessage(), Toast.LENGTH_SHORT).show();
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(getApplicationContext(), "Please select a location", Toast.LENGTH_SHORT).show();
            }
        }

    }

The problem is that I keep getting a toast showing The provided API key is invalid whenever I try to search for a location. 问题是我一直在试图显示当我尝试搜索位置时,提供的API密钥无效 How is this possible? 这怎么可能? The API key I got is completely new. 我得到的API密钥是全新的。

Edit: This is the error that I keep getting in my Logcat 编辑:这是我在Logcat中不断出现的错误

The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-04-29 19:36:10.285 14975-14975/com.example.myapplication E/Places: Error while autocompleting: REQUEST_DENIED

You are incorrectly passing the API Key. 您错误地传递了API密钥。

Replace this line: 替换此行:

Places.initialize(getApplicationContext(), "@string/google_place_api");

with: 有:

Places.initialize(getApplicationContext(), getString(R.string.google_place_api));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM