API access fails with "401 Login required"

4,019 views
Skip to first unread message

Christian Rishøj

unread,
Aug 24, 2015, 9:03:33 AM8/24/15
to Google Content API for Shopping

I'm in the process of setting up API access to Google Products. 

 

So far, I have

  1. a verified merchant account,
  2. an OAuth2 Client ID for an installed/native application (with client ID 1055046914708-nqsq03isi7tntv68gp7unis5270qups0.apps.googleusercontent.com),
  3. granted my own user administrative access in the Merchant Center, as well as
  4. granted (what I presume to be the Client ID user) 1055046914708-nqsq03isi...@developer.gserviceaccount.com administrative access in the Merchant Center.

Using the PHP API client, I authenticate using the JSON keyfile as follows:

 

$client = new Google_Client();
$client->setAuthConfigFile('/path/to/key.json');
$service = new Google_Service_ShoppingContent($client);

Yet, API calls such as

$service->products->insert($merchantId, $product, ['dryRun' => true]);

...fail with the following output:

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/content/v2/101033890/products?dryRun=true: (401) Login Required' in /path/to/vendor/google/apiclient/src/Google/Http/REST.php:110
Stack trace:
#0 /path/to/vendor/google/apiclient/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /path/to/vendor/google/apiclient/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /path/to/vendor/google/apiclient/src/Google/Http/REST.php(46): Google_Task_Runner->run()
#4 /path/to/vendor/google/apiclient/src/Google/Client.php(590): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#5 /path/to/vendor/google/apiclien in /Users/christian/Projects/imusic/vendor/google/apiclient/src/Google/Http/REST.php on line 110

What could I be doing wrong?

 

Thanks. 

Lakshmi (Content API Team)

unread,
Aug 24, 2015, 11:11:48 AM8/24/15
to Google Content API for Shopping
Hi Christian,
For installed applications you need to get the refresh token using the client ID and client secret to make authenticated and authorized API calls. Make sure you login with the right email when you are generating refresh token. In this case it would be your own user email address and not the service account email address. Service account email address is used for server to server applications. 

Thanks,
Lakshmi.


Christian Rishøj

unread,
Sep 8, 2015, 7:02:29 AM9/8/15
to Google Content API for Shopping

Hi Lakshmi,

In fact, this is a server-to-server application. 

Does the refresh tokens still apply in this case, or is there a simpler authentication route I can use?

Thanks,
Christian

Lakshmi (Content API Team)

unread,
Sep 8, 2015, 5:34:58 PM9/8/15
to Google Content API for Shopping
Hi Christian,

To clarify, were your Client credentials generated for installed application or service account? The reason I am asking is earlier you mentioned that credentials are generated for installed application.

Yes, you need to generate refresh tokens and add the right "email address" listed in the developer console to the "users" section of your Merchant Center Settings. Refer to our Get Started guide and  OAuth guide.

Thanks,
Lakshmi.

Christian Rishøj

unread,
Sep 9, 2015, 6:30:09 AM9/9/15
to Google Content API for Shopping
Hi Lakshmi,

I had credentials for an installed application, although a service account is more appropriate for my use case.

Accessing the API works well with service account credentials and the following snippet for authentication:

$client = new Google_Client();
$cred = $client->loadServiceAccountJson('/path/to/google_api.json', ['https://www.googleapis.com/auth/content']);
$oauthClient = new Google_Auth_OAuth2($client);
$oauthClient->refreshTokenWithAssertion($cred);
$client->setAccessToken($oauthClient->getAccessToken());
$service = new Google_Service_ShoppingContent($client);

Thanks!

Lakshmi (Content API Team)

unread,
Sep 9, 2015, 4:09:47 PM9/9/15
to Google Content API for Shopping
Hi Christian,

I noticed in your other post, you were able to make API calls. Is this issue resolved or do you still need any help?

Thanks,
Lakshmi.

Christian Rishøj

unread,
Sep 9, 2015, 5:51:03 PM9/9/15
to google-content-...@googlegroups.com
Hi Lakshmi,

The issues are resolved. 

Thank you!

--
You received this message because you are subscribed to a topic in the Google Groups "Google Content API for Shopping" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-content-api-for-shopping/F5-CNNMkhDU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-content-api-fo...@googlegroups.com.
To post to this group, send email to google-content-...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-content-api-for-shopping.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages