Search code examples
phpgoogle-apigoogle-shopping-apigoogle-shopping

How to programmatically get the access token to send API call to Google Merchant Center (Google Shopping) with PHP?


I recently get in touch with Google merchant product to sync all my website products into Google Merchant. When I follow the structure of the API documentation https://developers.google.com/shopping-content/v2/quickstart down to Authorization section I copy their library and copy the sample code to use. It actually work! However, when I doing testing to load that auth page it requires me to log in to the developer account for getting the access token and save it into session.

Is there any possibility that I can skip the login section to make it automatically then I can do corn system to run the sync (update products' details) hourly?

I tried to hardcore my account login API key into my code like this:

$client = new Google_Client();
$client->setApplicationName('Sample Content API application');

//add my api key here
$client->setDeveloperKey(MY_API_KEY);

$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setScopes('https://www.googleapis.com/auth/content');

But it doesn't workЖ шt still requires me to log in.


Solution

  • You want to use a service account for this.

    The Google OAuth 2.0 system supports server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)