user3792981
user3792981

Reputation: 11

authentication Google Content Api service account - error 'user cannot access account'

I am bashing my head against the wall trying to get my code to work. I already have succesfully connected to the google Api by using the authentication route with the redirect url.

However, I need to get this working as a standalone service which will be running in a crontab.

So now I am trying to authenticate with a 'service account'.

This is my code and below that you will find the error it is giving me.

$client = new Google_Client();
$client->setApplicationName('GoogleShoppingService');

if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
}

$service = new Google_Service_Content($client);

$client_id = 'xxxxxxx.apps.googleusercontent.com';
$client->setClientId($client_id);
$service_account_name = '[email protected]';


$key_file_location = 'privatekey.p12';
$key = file_get_contents($key_file_location);
$scope = 'https://www.googleapis.com/auth/content';
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
        array($scope),
        $key
    );
$result = $client->setAssertionCredentials($cred);
print_r($result);
if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

$_SESSION['service_token'] = $client->getAccessToken();
echo $client->getAccessToken();

$merchantId = 1234567;//this a dummy value, I replace it with the actual merchantId
$accountId = 1234567;//this a dummy value, I replace it with the actual accountId
$accountstatuses = $service->accountstatuses->get($merchantId,$accountId);

So this is the message I get when running the code. (note that the token is created, but that I am not able to access the content of the Google Shopping feed.)

{"access_token":"ya29.NQD7MQz0cas9PhoAAADRPMlTVecqYXYh4fNoZfRMymQtSF4hwqJn31uobohLbw","expires_in":3600,"created":1404200605}
Fatal error:
Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/content/v2/1234567/accountstatuses/1234567: (401) User cannot access account 1234567' in /home/sites/site1/web/googleShopping2/src/Google/Http/REST.php:79
Stack trace:
#0 /home/sites/site1/web/googleShopping2/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 /home/sites/site1/web/googleShopping2/src/Google/Client.php(499): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 /home/sites/site1/web/googleShopping2/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
#3 /home/sites/site1/web/googleShopping2/src/Google/Service/Content.php(685): Google_Service_Resource->call('get', Array, 'Google_Service_...')
#4 /home/sites/site1/web/googleShopping2/examples/test.php(58): Google_Service_Content_Accountstatuses_Resource->get(1234567, 1234567)
#5 {main} thrown in /home/sites/site1/web/googleShopping2/src/Google/Http/REST.php on line 79

I've already tried to find a solution for 2 days now, but any hint that I've found doesn't help me. I've also creted a new project with new Api auth keys, but nothing works and I always get the above message. Who can help me please?

Regards, Martin

Upvotes: 1

Views: 2357

Answers (2)

Mo D Genesis
Mo D Genesis

Reputation: 6169

On https://merchants.google.com/

  • At the top right, click on the gear icon i.e settings.
  • click on account access
  • click +add user
  • add the email-adress you got from the service account .json file. (it's in there)
  • make sure the added user is set to admin.

Upvotes: 2

tuurbo
tuurbo

Reputation: 361

You need to add the email address of the service account you created to the Google merchant account under Settings > User.

Upvotes: 5

Related Questions