Google Books API - 继续收到错误代码“403”原因:“ipRefererBlocked” [英] Google Books API - keep getting error code "403" reason: "ipRefererBlocked"

查看:165
本文介绍了Google Books API - 继续收到错误代码“403”原因:“ipRefererBlocked”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个作为我的请求url:

 `String isbnUrl =https://www.googleapis.com / books / v1 / volumes?q = isbn:+ isbn +& key =+ myAPIKEY;`

任何人都可以告诉我为什么我不断收到此回复:

  {
error:{
错误:[
{
domain:usageLimits,
reason:ipRefererBlocked,
message: -IP或per-Referer限制在您的API密钥上配置,并且请求与这些限制不符。请允许使用Google Developers Console更新您的API密钥配置,如果允许来自此IP或引用者的请求。,
extendedHelp:https://console.developers.google.com
}
],
code:403,
message:There is a per-IP或per-Referer限制,并且请求不符合这些限制,请使用t如果应该允许来自此IP或引荐人的请求,他可以通过Google Developers Console更新您的API密钥配置。
}
}

我已经完成了获取API的过程我的Android应用程序使用调试密钥库和发布密钥库,似乎无法得到它的工作我已经尝试添加我的密钥作为标题作为建议作为答案在这里: Google Books API 403未配置访问权限
我以为这是答案,但后来偶然意识到它和没有提供钥匙一样。输入错误的字符串作为关键字后,我意识到这一点,它仍然有效。



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



如果有人知道如何通过包含密钥来让API以Google想要的方式工作,那么我真的很感激任何帮助。 解决方案 问题是在设置API应用程序的API密钥限制时,您指定了软件包名称和SHA-因此,您的API密钥只会接受您的应用程序发出的包名称和指定的SHA-1证书指纹的请求。 因此,当您向Google发送请求时,您必须添加这些信息在每个请求的标题中包含以下关键字:



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



键:X-Android-Cert,值:SHA-1证书您的apk



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

  / ** 
*获取SHA1签名,十六进制编码用于包含Google Cloud Platform API请求
*
* @param packageName标识要提取签名的APK 。
* @返回一个小写,十六进制编码的
* /
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;
}
返回signatureDigest(packageInfo.signatures [0]);
} catch(PackageManager.NameNotFoundException e){
return null;



private static String signatureDigest(Signature sig){
byte [] signature = sig.toByteArray();
尝试{
MessageDigest md = MessageDigest.getInstance(SHA1);
byte [] digest = md.digest(signature);
返回BaseEncoding.base16()。lowerCase()。encode(digest);
} catch(NoSuchAlgorithmException e){
return null;


$ / code>

然后,添加软件包名称和SHA证书签名以请求header:

  java.net.URL url = new URL(REQUEST_URL); 
HttpURLConnection连接=(HttpURLConnection)url.openConnection();
尝试{
connection.setDoInput(true);
connection.setDoOutput(true);

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

//将软件包名称添加到请求头文件
String packageName = mActivity.getPackageName();
connection.setRequestProperty(X-Android-Package,packageName);
//添加SHA证书来请求头文件
String sig = getSignature(mActivity.getPackageManager(),packageName);
connection.setRequestProperty(X-Android-Cert,sig);
connection.setRequestMethod(POST);

//将您的请求添加到此处
// ....................
} catch(Exception e ){
e.printStackTrace();
} finally {
connection.disconnect();

你可以看到

$ b








$ b <享受编码:D


I am using this as my request url:

`String isbnUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn + "&key=" + myAPIKEY;`

Can anyone tell me why I keep getting this response:

{
   "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."
   }
}

I have gone through the process of getting an API for my Android app using the debug keystore and release keystore and can't seem to get it to work I have tried adding my key as a header as suggested as an answer here: Google Books API 403 Access Not Configured.
I thought this was the answer but then realized by accident that it was the same as not supplying a key at all. I came to this realization after entering the wrong String as the key and it still worked.

In the developer console I am seeing that it receives the request from my API under usage response code section: Client errors (4xx).

I would really appreciate any help if anyone has figured out how to get this API to work the way Google wants by including the key.

解决方案

Problem is when setting up your API key restriction for android app, you specified the package name and SHA-1 certificate fingerprint. Therefore your API key will only accept request from your app with package name and SHA-1 certificate fingerprint specified.

So when you send an request to Google, you MUST add these information in the header of each request with following keys:

Key: "X-Android-Package", value: your app package name

Key: "X-Android-Cert", value: SHA-1 certificate of your apk

FIRST, get your app SHA signature (you will need Guava library):

/**
 * 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;
    }
}

Then, add package name and SHA certificate signature to request header:

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();
}

You can see full answer here.

Enjoy coding :D

这篇关于Google Books API - 继续收到错误代码“403”原因:“ipRefererBlocked”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆