我使用这个作为我的请求 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."
}
}
我已经完成了使用调试 keystore 和发布 keystore 为我的 Android 应用程序获取 API 的过程,但似乎无法让它工作我已经尝试按照建议将我的 key 添加为 header 作为答案: Google Books API 403 Access Not Configured .
我以为这就是答案,但后来偶然意识到这与根本不提供 key 是一样的。我在输入错误的字符串作为键后才意识到这一点,但它仍然有效。
在开发人员控制台中,我看到它在使用响应代码部分收到来 self 的 API 的请求:客户端错误 (4xx)。
如果有人知道如何通过包含 key 让此 API 以 Google 想要的方式工作,我将不胜感激。
最佳答案
问题是在为 Android 应用程序设置 API key 限制时,您指定了程序包名称和 SHA-1 证书指纹。因此,您的 API key 将只接受来自您的应用程序的请求,其中指定了程序包名称和 SHA-1 证书指纹。
因此,当您向 Google 发送请求时,您必须使用以下键将这些信息添加到每个请求的 header 中:
Key: "X-Android-Package"
, value: 你的应用包名
key :"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();
}
可以看到full answer here .
享受编码:D
关于android - Google Books API - 不断收到错误代码 "403"原因 : "ipRefererBlocked",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246452/