Php GSC_客户端和oAuth2访问
我开始编写一个PHP脚本,它将作为cron作业运行,并通过Google购物API定期更新产品列表 我下载了,并试图通过工作,以获得尽可能多的令牌。然而,在文档中似乎缺少了一个步骤,即在生成URL后如何实际请求令牌 以下是我目前的代码:Php GSC_客户端和oAuth2访问,php,google-api,oauth-2.0,google-shopping-api,Php,Google Api,Oauth 2.0,Google Shopping Api,我开始编写一个PHP脚本,它将作为cron作业运行,并通过Google购物API定期更新产品列表 我下载了,并试图通过工作,以获得尽可能多的令牌。然而,在文档中似乎缺少了一个步骤,即在生成URL后如何实际请求令牌 以下是我目前的代码: require ("./lib/shoppingclient/GShoppingContent.php"); const G_MERCHANT_ID = '**********'; const G_CLIENT_ID = '*********
require ("./lib/shoppingclient/GShoppingContent.php");
const G_MERCHANT_ID = '**********';
const G_CLIENT_ID = '**********';
const G_CLIENT_SECRET = '**********';
$obj_client = new GSC_Client (G_MERCHANT_ID);
// Obtain an OAuth2 token to access the API with
$obj_token = new GSC_OAuth2Token (G_CLIENT_ID, G_CLIENT_SECRET, USER_AGENT);
$str_url = $obj_token -> generateAuthorizeUrl ('urn:ietf:wg:oauth:2.0:oob');
echo ($str_url . PHP_EOL);
/* @var $obj_response _GSC_Response */
$obj_response = $obj_token -> makeAuthenticatedRequest (curl_init ($str_url));
echo ($obj_response);
当我从命令行运行上述命令时,我得到:
当我运行上述代码时,我得到了类似于以下内容的回报:
stdClass Object
(
[access_token] => ya29.AHES6ZRJohl2AfbQCKbFxNlagSqLGcjHwiylqASX1ygmwg
[expires_in] => 3600
[created] => 1359123809
)
我猜这是一个有效的访问令牌响应
但是,我还没有弄清楚如何在GSC_客户端库中使用返回的令牌。虽然我知道这两个库都起源于谷歌,但我有一个明显的印象,那就是它们是由不同的团队开发的,这些团队彼此之间几乎没有什么关系,最终的结果是这些库不能相互兼容。如果有人知道在这里该做什么,我会感谢你的任何建议
更新3
我已经设法使用oAuth库从Google中提取数据,但它来自购物搜索API。我需要用购物的内容API操作产品列表。即使在contrib目录中,oAuth库似乎也没有提供这样做的类
仅供参考,以下是执行搜索API请求的代码(减去常量):
看起来GSC_客户端库与其自己的OAuth2实现的耦合过于紧密,Google_客户端库无法轻松集成到其中,而无需进行大量重构工作。此外,GSC_客户机的OAuth2实现与客户机机密概念和重定向到同意页的关系过于紧密,因此我无法编写一个实现,将Google_客户机实现包装为替换 幸运的是,我们有一个代码库,用于与谷歌内部开发的API进行接口,并且与认证系统的耦合不太紧密。只需做一点工作,就可以为它添加一个新的身份验证模块来包装Google_客户端
然而,对于如何让Google的GSC_客户端与Google_客户端协同工作,似乎还没有一个答案,所以我仍然在寻找更好的答案 我知道这是一个有点老的问题,但我有类似的要求,因为谷歌刚刚终止了我们用来更新价格和可用性的旧API 下面的代码对我有效。它使用服务器身份验证,因此不需要用户输入。您将需要登录到您的google api控制台并设置一个新的服务帐户(或者使用现有的服务帐户,如果您有一个和p12文件),此外,您还需要将服务代码的电子邮件地址作为标准用户添加到您的google Merchant Center中-嗯,我不确定是否需要,但我已经这样做了,它可以工作:-) 另一方面,使用旧的API,我们使用MPN和新的AP进行搜索
require_once realpath(dirname(__FILE__) . '/src/Google/autoload.php');
require_once realpath(dirname(__FILE__) . '/src/Google/Service/ShoppingContent.php');
$merchantId = '<<MY_MERCHANT_ID>>';
$client_id = '<<MY_GOOGLE_API_SERVICE_ACCOUNT_ID>>';
$client_email = <<MY_GOOGLE_API_SERVICE_ACCOUNT_EMAIL>>';
$scopes = array('https://www.googleapis.com/auth/content');
$private_key = file_get_contents('<<MY_GOOGLE_API_SERVICE_ACCOUNT_P12_FILE>>');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
$client->setScopes($scopes);
$client->setAccessType("offline");
if ($client->getAuth()->isAccessTokenExpired()) $client->getAuth()->refreshTokenWithAssertion();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
$service = new Google_Service_ShoppingContent($client);
//Example to get sku information
$ret = getsku($client, $service, $merchantId, 'DC35DS');
echo "<pre>"; print_r($ret); echo "</pre>";
//Example to set price and availability
$ret = update_Price_Availability($client, $service, $merchantId, $itemid, $price, $availability);
echo "<pre>"; print_r($ret); echo "</pre>";
function update_Price_Availability($client, $service, $merchantId, $itemid, $newprice = null, $availability = null) {
$inventory = new Google_Service_ShoppingContent_InventorySetRequest();
$price = new Google_Service_ShoppingContent_Price();
$ctrl = 0;
if ($newprice !== null) {
$price->setValue($newprice);
$price->setCurrency('GBP');
$inventory->setPrice($price);
$ctrl = 1;
}
if ($availability !== null) {
$inventory->setAvailability($availability);
$ctrl = 1;
}
if ($ctrl == 0) {
return array('Errors'=>array('Nothing to do'));
}
try {
$return = $service->inventory->set($merchantId, 'online', 'online:en:GB:'.$itemid, $inventory);
} catch (Google_Service_Exception $e) {
return array('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
return getsku($client, $service, $merchantId, $itemid);
}
function getsku($client, $service, $merchantId, $itemid) {
try {
$product = $service->products->get($merchantId, 'online:en:GB:'.$itemid);
} catch (Google_Service_Exception $e) {
$product = array('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
return $product;
}
require_once realpath(dirname(__文件)。'/src/Google/autoload.php');
需要_once realpath(dirname(__文件)'/src/Google/Service/ShoppingContent.php');
$merchantId='';
$client_id='';
$client_email=';
$scopes=数组('https://www.googleapis.com/auth/content');
$private\u key=文件获取内容(“”);
$credentials=新的谷歌认证断言(
$client_电子邮件,
$scopes,
$private_key
);
$client=新的Google_客户端();
$client->setAssertionCredentials($credentials);
$client->setScopes($scopes);
$client->setAccessType(“脱机”);
如果($client->getAuth()->isAccessTokenExpired())$client->getAuth()->refreshTokenWithAssertion();
如果(isset($\会话['access\ u token'])&&$\会话['access\ u token'])){
$client->setAccessToken($_会话['access_token']);
}否则{
$authUrl=$client->createAuthUrl();
}
$service=新的谷歌服务\购物内容($client);
//获取sku信息的示例
$ret=getsku($client,$service,$merchantId,'DC35DS');
回声“;印刷费($ret);回声“;
//设置价格和可用性的示例
$ret=更新价格可用性($client、$service、$merchantId、$itemid、$Price、$Availability);
回声“;印刷费($ret);回声“;
函数更新价格可用性($client、$service、$merchantId、$itemid、$newprice=null、$Availability=null){
$inventory=新谷歌\服务\购物内容\库存设置请求();
$price=谷歌新服务\购物内容\价格();
$ctrl=0;
如果($newprice!==null){
$price->setValue($newprice);
$price->setCurrency('GBP');
$inventory->setPrice($price);
$ctrl=1;
}
如果($availability!==null){
$inventory->setAvailability($availability);
$ctrl=1;
}
如果($ctrl==0){
返回数组('Errors'=>array('notodo-do'));
}
试一试{
$return=$service->inventory->set($merchantId,'online','online:en:GB:'。$itemid,$inventory);
}捕获(谷歌服务例外$e){
返回数组('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
返回getsku($client、$service、$merchantId、$itemid);
}
函数getsku($client、$service、$merchantId、$itemid){
试一试{
$product=$service->products->get($merchantId,'online:en:GB:'。$itemid);
}捕获(谷歌服务例外$e){
$product=array('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
退回$product;
}
你能分享一下坏掉的东西吗。一旦您拥有针对structuredcontent作用域授权的访问令牌,就可以通过GET/POST请求将其传递给产品API。你能分享一下确切的错误吗?什么是“打破”的是,似乎没有任何方法将从google_客户端库获得的令牌注入GSC_内容库。他们显然不相容。
stdClass Object
(
[access_token] => ya29.AHES6ZRJohl2AfbQCKbFxNlagSqLGcjHwiylqASX1ygmwg
[expires_in] => 3600
[created] => 1359123809
)
$obj_client_auth = new Google_Client ();
$obj_client_auth -> setApplicationName ('test');
$obj_client_auth -> setClientId (G_CLIENT_ID);
$obj_client_auth -> setAssertionCredentials (new Google_AssertionCredentials (
G_CLIENT_EMAIL,
array (
//'https://www.googleapis.com/auth/structuredcontent',
'https://www.googleapis.com/auth/shoppingapi'
),
file_get_contents (G_CLIENT_KEY_PATH),
G_CLIENT_KEY_PW));
$obj_client_api = new Google_ShoppingService ($obj_client_auth);
$arr_results = $obj_client_api -> products -> listProducts ('public', array (
'country' => 'GB',
'q' => '"mp3 player" | ipod',
'rankBy' => 'relevancy'
));
print_r ($arr_results);
require_once realpath(dirname(__FILE__) . '/src/Google/autoload.php');
require_once realpath(dirname(__FILE__) . '/src/Google/Service/ShoppingContent.php');
$merchantId = '<<MY_MERCHANT_ID>>';
$client_id = '<<MY_GOOGLE_API_SERVICE_ACCOUNT_ID>>';
$client_email = <<MY_GOOGLE_API_SERVICE_ACCOUNT_EMAIL>>';
$scopes = array('https://www.googleapis.com/auth/content');
$private_key = file_get_contents('<<MY_GOOGLE_API_SERVICE_ACCOUNT_P12_FILE>>');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
$client->setScopes($scopes);
$client->setAccessType("offline");
if ($client->getAuth()->isAccessTokenExpired()) $client->getAuth()->refreshTokenWithAssertion();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
$service = new Google_Service_ShoppingContent($client);
//Example to get sku information
$ret = getsku($client, $service, $merchantId, 'DC35DS');
echo "<pre>"; print_r($ret); echo "</pre>";
//Example to set price and availability
$ret = update_Price_Availability($client, $service, $merchantId, $itemid, $price, $availability);
echo "<pre>"; print_r($ret); echo "</pre>";
function update_Price_Availability($client, $service, $merchantId, $itemid, $newprice = null, $availability = null) {
$inventory = new Google_Service_ShoppingContent_InventorySetRequest();
$price = new Google_Service_ShoppingContent_Price();
$ctrl = 0;
if ($newprice !== null) {
$price->setValue($newprice);
$price->setCurrency('GBP');
$inventory->setPrice($price);
$ctrl = 1;
}
if ($availability !== null) {
$inventory->setAvailability($availability);
$ctrl = 1;
}
if ($ctrl == 0) {
return array('Errors'=>array('Nothing to do'));
}
try {
$return = $service->inventory->set($merchantId, 'online', 'online:en:GB:'.$itemid, $inventory);
} catch (Google_Service_Exception $e) {
return array('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
return getsku($client, $service, $merchantId, $itemid);
}
function getsku($client, $service, $merchantId, $itemid) {
try {
$product = $service->products->get($merchantId, 'online:en:GB:'.$itemid);
} catch (Google_Service_Exception $e) {
$product = array('Errors'=>$e->geterrors(),'Message'=>$e->getmessage());
}
return $product;
}