Google Books API - 不断收到错误代码"403"原因:"ipRefererBlocked"



我使用此作为我的请求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."
   }
}

我已经完成了使用调试密钥库和发布密钥库为我的Android应用程序获取API的过程,但似乎无法使其工作。我尝试将我的密钥添加为头,作为这里的答案:Google Books API 403 Access Not Configured
我以为这就是答案,但后来意外地意识到,这与根本不提供钥匙是一样的。我是在输入错误的String作为键后才意识到这一点的,它仍然有效。

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

如果有人找到了如何让这个API按照谷歌想要的方式工作,包括密钥,我将非常感谢任何帮助。

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

因此,当你向谷歌发送请求时,你必须在每个请求的标题中添加这些信息,并使用以下密钥:

密钥:"X-Android-Package",值:您的应用程序包名称

密钥:"X-Android-Cert",值:apk 的SHA-1证书

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

/**
 * Gets the SHA1 signature, hex encoded for inclusion with Google Cloud Platform API requests
 *
 * @param packageName Identifies the APK whose signature should be extracted.
 * @return a lowercase, hex-encoded
 */
public static String getSignature(@NonNull PackageManager pm, @NonNull String packageName) {
    try {
        PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        if (packageInfo == null
                || packageInfo.signatures == null
                || packageInfo.signatures.length == 0
                || packageInfo.signatures[0] == null) {
            return null;
        }
        return signatureDigest(packageInfo.signatures[0]);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }
}
private static String signatureDigest(Signature sig) {
    byte[] signature = sig.toByteArray();
    try {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] digest = md.digest(signature);
        return BaseEncoding.base16().lowerCase().encode(digest);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

然后,将包名称和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

相关内容

  • 没有找到相关文章

最新更新