Google Books API:“无法确定受地理位置限制的操作的用户位置." [英] Google Books API: "Cannot determine user location for geographically restricted operation."

查看:133
本文介绍了Google Books API:“无法确定受地理位置限制的操作的用户位置."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

三天以来,尽管我的IP并未更改,但在尝试访问Google Books API时收到了上述错误消息.我可以在命令行上用一个简单的方法重现它

Since three days I get the above mentioned error message when trying to access google books api, although my IP didn't change. I can reproduce that on the command line with a simple

curl "https://www.googleapis.com/books/v1/volumes?q=frankenstein"

所以这不是我的代码.可以通过添加国家代码来解决:

So it's not my code. It can be fixed adding a country code:

curl "https://www.googleapis.com/books/v1/volumes?q=frankenstein&country=DE"

现在如何在PHP客户端中做到这一点?

Now how do I do that in the PHP client?

我尝试添加国家/地区作为可选参数:

I tried adding country as an optional Parameter:


$client = new Google_Client();
$client->setApplicationName("My_Project");
$client->setDeveloperKey( $google_books_api_key );
$service = new Google_Service_Books($client);
$optParams = array(
    'country' => 'DE'
);
$results = $service->volumes->listVolumes($terms, $optParams);

但这只是给我

{"error": {"errors": [{"domain": "global","reason": "backendFailed","message": "Service temporarily unavailable.","locationType": other","location": "backend_flow"}],"code": 503,"message": "Service emporarily anavailable."}}

我找到了一种将用户IP设置为我可以访问的IP地址的解决方案,但仍然给了我地理位置受限"的错误消息.

A solution I found somwhere to set the users IP to one I do have access from still gave me the 'geographically restricted' error message.

$optParams = array(
    'userIp' => '91.64.137.131'
);

我找到了PHP以外的客户端解决方案,例如 Ruby

I found solutions for clients other then PHP like Java? or Ruby or C# but they didn't seem helpful to me.

在PHP客户端中,类Google_Service_Books_VolumeAccessInfo扩展了Google_Model"中存在一个 setCountry($ country)方法,但是我不知道如何访问该方法.有人知道如何解决吗?

In the PHP client a setCountry($country) method exists in 'class Google_Service_Books_VolumeAccessInfo extends Google_Model' but I don't know how to access that method. Does anyone know how to solve that?

推荐答案

您可以通过与使用中间件共享的其他语言示例类似的方式来完成此操作:

You can accomplish this in a manner similar to the other language examples you shared by using middlewares:

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Middleware;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

// Set this value to the country you want.
$countryCode = 'DE';

$client = new Google_Client();
$client->setApplicationName("My_Project");
$client->setDeveloperKey( $google_books_api_key );
$service = new Google_Service_Books($client);
$optParams = [];

$handler = new CurlHandler;
$stack = HandlerStack::create($handler);
$stack->push(Middleware::mapRequest(function ($request) use ($countryCode) {
    $request = $request->withUri(Uri::withQueryValue(
        $request->getUri(),
        'country',
        $countryCode
    ));

    return $request;
}));
$guzzle = new Client([
    'handler' => $stack
]);

$client->setHttpClient($guzzle);

$results = $service->volumes->listVolumes($terms, $optParams);

中间件是一组用于修改请求和响应的功能.此示例添加了一个请求中间件,该中间件在分派请求之前会将country=$countryCode添加到URI查询字符串中.

Middleware is a set of functions which are used to modify requests and responses. This example adds a request middleware, which prior to dispatching the request, will add country=$countryCode to the URI query string.

该示例在某种程度上得到了简化,您将需要对其进行一些处理.最大的问题是该中间件会将国家代码添加到从此Google_Client实例发送的每个请求中.我建议添加其他逻辑以仅将修改限制为该请求.

This example is simplified to some extent, and you'll need to work on it a bit. The big issue is that this middleware will add the country code to every request sent from this instance of Google_Client. I suggest adding additional logic to limit the modification to this request only.

这篇关于Google Books API:“无法确定受地理位置限制的操作的用户位置."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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