【问题标题】:This IP, site or mobile application is not authorized to use this API key Request received from IP address 163.85.31.144, with empty referer此 IP、站点或移动应用程序无权使用此 API 密钥 从 IP 地址 163.85.31.144 收到的请求,引用者为空
【发布时间】:2016-03-27 09:43:39
【问题描述】:

我正在尝试连接到 google places api,但它给了我错误: "此 IP、站点或移动应用程序无权使用此 API 密钥。从 IP 地址 183.87.41.123 收到请求,引用为空", "predictions" : [], "status" : "REQUEST_DENIED"}

我已经使用了所有三种类型的 API,但它不工作首先我使用 Android 密钥,然后我使用浏览器密钥,现在我使用服务器密钥但没有结果!

这是我的代码:

package com.astro.famouspandit.Activities.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.astro.famouspandit.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

public class Location extends AppCompatActivity  {

    private EditText mEdttxtCity;
    private String input;
     String API_KEY;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
         //API_KEY = "sdlflfNK-sldkfndlsnfdslanflsdn";
         API_KEY = "fsdnflsdnflnflsdnfldsnfldnfldnf";
         //browser key API_KEY = "askjskfdskjfssdkfksdjbfsdk-csdkfdskn";

        mEdttxtCity = (EditText)findViewById(R.id.edttxtCity);
        mEdttxtCity.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 input =  mEdttxtCity.getText().toString();
                if (input.length() >3) {

                    SearchPlacesTask   searchplacesTask = new SearchPlacesTask();
                    searchplacesTask.execute();


                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }



        });




    }
    private class SearchPlacesTask extends AsyncTask<String, Void, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.d("SearchPlacesTask Called","");

        }


        @Override
        protected String doInBackground(String... place) {
            // For storing data from web service
            String data = "";

            // Obtain browser key from https://code.google.com/apis/console

            String key = "key="+API_KEY;


            try {
                input = "input=" + URLEncoder.encode(input, "utf-8");
                Log.e("input String : ","input" + input);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            // place type to be searched
            String types = "types=geocode";

            // Sensor enabled
            String sensor = "language=en";

                /*// Building the parameters to the web service
                String parameters = input + "&" + types + "&" + sensor + "&" + key;*/


            // Building the parameters to the web service

            String parameters = input + "&" + sensor + "&" + key;


            // Output format
            String output = "json";

            // Building the url to the web service

            String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"
                    + output + "?" + parameters;

            Log.d("Final URL Google API : ","url" + url);
            //            String url = "https://maps.googleapis.com/maps/api/place/autocomplete/location=13.758662,100.496443" + output + "?" + parameters;

            try {
                // Fetching the data from we service
                data = downloadUrlsearch(url);
            } catch (Exception e) {
                Log.d("Background Task", e.toString());

            }

            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

//YOUR JSON DATA HERE.
//NOW YOU CAN PARSE.


        }
    }

    private String downloadUrlsearch(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try {

            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();


            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();

            Log.d("data: ","" + data);

            br.close();


        } catch (Exception e) {

            e.printStackTrace();
//            Log.d("Exception while downloading url", e.toString());
        } finally {

            iStream.close();
            urlConnection.disconnect();
        }
        return data;
    }


}

【问题讨论】:

标签: android google-maps


【解决方案1】:

确保在生成 API 时没有将 API 指定为特定于某个包或签名?

【讨论】:

  • 你能详细说明一下吗?我使用我的 IP 地址创建服务器密钥,我使用包名称 com.astro.woild 作为创建浏览器密钥的包
  • 你能告诉我我必须使用哪种键类型吗?
  • 您必须在单击“创建凭据”时生成 android 密钥。此外,当您在创建 APi 密钥时提及您的包名称时,请检查 SHA-1 证书指纹是否正确。
猜你喜欢
  • 2021-10-29
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2014-06-03
相关资源
最近更新 更多