I am starting work on a PHP script that will run as a cron job and periodically update product lists through the Google Shopping API.
I downloaded the GSC client library for PHP and am trying to work through the Google Shopping API to access the token. However, it seems that somewhere in the docs some kind of step has gone missing regarding how to actually request a token after creating the URL.
Here is my code:
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);
$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);
$obj_response = $obj_token -> makeAuthenticatedRequest (curl_init ($str_url));
echo ($obj_response);
When I run the above from the command line, I get:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=blah-blah-blah-etc-etc-etc ...
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Fatal error: Uncaught exception 'GSC_TokenError' with message 'invalid_request' in /var/samba/GoogleShoppingTest/lib/shoppingclient/GShoppingContent.php on line 624
GSC_TokenError: invalid_request in /var/samba/GoogleShoppingTest/lib/shoppingclient/GShoppingContent.php on line 624
Call Stack:
0.0002 321888 1. {main}() /var/samba/GoogleShoppingTest/logintest.php:0
0.0065 1446196 2. GSC_OAuth2Token->makeAuthenticatedRequest() /var/samba/GoogleShoppingTest/logintest.php:19
0.2797 1446684 3. GSC_OAuth2Token->refresh() /var/samba/GoogleShoppingTest/lib/shoppingclient/GShoppingContent.php:722
0.3992 1448152 4. GSC_OAuth2Token::raiseFromJson() /var/samba/GoogleShoppingTest/lib/shoppingclient/GShoppingContent.php:565
, CURL , , URL- .
URL-, generateAuthorizeUrl(), , . , , , , , .
cron script, , , , , , , - .
- GSC_Client OAuth script? , ?
. API " ", , -, API . , script https://localhost urn:ietf:wg:oauth:2.0:oob URL-.
2. , GSC "-". , API Google, .
, :
require ("./lib/google/oauthclient/Google_Client.php");
require ("./lib/google/shoppingclient/GShoppingContent.php");
const G_MERCHANT_ID = '********';
const G_CLIENT_ID = '********';
const G_CLIENT_EMAIL = '********';
const G_CLIENT_KEY_PATH = '/path/to/the/privatekey.p12';
const G_CLIENT_KEY_PW = 'notasecret';
$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 (OAUTH_SCOPE),
file_get_contents (G_CLIENT_KEY_PATH),
G_CLIENT_KEY_PW));
$obj_client_auth -> getAuth () -> refreshTokenWithAssertion ();
$obj_token = json_decode ($obj_client_auth -> getAccessToken ());
print_r ($obj_token);
, - :
stdClass Object
(
[access_token] => ya29.AHES6ZRJohl2AfbQCKbFxNlagSqLGcjHwiylqASX1ygmwg
[expires_in] => 3600
[created] => 1359123809
)
, .
, GSC_Client. , Google, , , , , -, - , , , .
3
oAuth, Google, API . Content API . , , oAuth, , contrib!
FYI, , API ( ):
$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/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);