Android 谷歌图书API-不断获取错误代码“;403“;理由是:;“IPRefererBlock”;

Android 谷歌图书API-不断获取错误代码“;403“;理由是:;“IPRefererBlock”;,android,google-api,Android,Google Api,我正在使用此作为我的请求url: `String isbnUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn + "&key=" + myAPIKEY;` 有谁能告诉我为什么我总是得到这样的回答: { "error":{ "errors":[ { "domain":"usageLimits", "reason":"ipRe

我正在使用此作为我的请求url:

`String isbnUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn + "&key=" + myAPIKEY;`
有谁能告诉我为什么我总是得到这样的回答:

{
   "error":{
      "errors":[
         {
            "domain":"usageLimits",
            "reason":"ipRefererBlocked",
            "message":"There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
            "extendedHelp":"https://console.developers.google.com"
         }
      ],
      "code":403,
      "message":"There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
   }
}
我已经使用debug密钥库和release密钥库为我的Android应用程序获取了一个API,但似乎无法正常工作。我已尝试将我的密钥添加为标题,如此处建议的答案所示:。
我以为这就是答案,但后来意外地意识到,这与根本不提供钥匙是一样的。我是在输入了错误的字符串作为键后意识到这一点的,它仍然有效

在开发人员控制台中,我看到它在使用响应代码部分:客户端错误(4xx)下接收来自API的请求


如果有人知道如何让这个API按照Google希望的方式工作,包括密钥,我将非常感谢任何帮助

问题是,在为android应用程序设置API密钥限制时,您指定了包名和SHA-1证书指纹。因此,您的API密钥将只接受来自您的应用程序的请求,并指定包名和SHA-1证书指纹

因此,当您向Google发送请求时,必须使用以下键将这些信息添加到每个请求的标题中:

键:
“X-Android-Package”
,值:您的应用程序包名称

密钥:
“X-Android-Cert”
,值:您的apk的SHA-1证书

首先,获取你的应用程序SHA签名(你需要Guava library):

然后,将包名和SHA证书签名添加到请求头:

java.net.URL url = new URL(REQUEST_URL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
    connection.setDoInput(true);
    connection.setDoOutput(true);

    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    connection.setRequestProperty("Accept", "application/json");

    // add package name to request header
    String packageName = mActivity.getPackageName();
    connection.setRequestProperty("X-Android-Package", packageName);
    // add SHA certificate to request header
    String sig = getSignature(mActivity.getPackageManager(), packageName);
    connection.setRequestProperty("X-Android-Cert", sig);
    connection.setRequestMethod("POST");

    // ADD YOUR REQUEST BODY HERE
    // ....................
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}
你可以看到

喜欢编码:D

java.net.URL url = new URL(REQUEST_URL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
    connection.setDoInput(true);
    connection.setDoOutput(true);

    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    connection.setRequestProperty("Accept", "application/json");

    // add package name to request header
    String packageName = mActivity.getPackageName();
    connection.setRequestProperty("X-Android-Package", packageName);
    // add SHA certificate to request header
    String sig = getSignature(mActivity.getPackageManager(), packageName);
    connection.setRequestProperty("X-Android-Cert", sig);
    connection.setRequestMethod("POST");

    // ADD YOUR REQUEST BODY HERE
    // ....................
} catch (Exception e) {
    e.printStackTrace();
} finally {
    connection.disconnect();
}