1

I own a site and would like to place my products on Google Shopping, I currently use Google spreadsheets, but I would like to do this through integration. To import my products automatically. For this I verified that I need to use OAuth, but I am not able to make it work. I can not find where the error might be.

Code PHP:

<?php
require_once __DIR__ . '/vendor/autoload.php';

define('MERCHANT_ID', 'xxxxxxxxx');
session_start();

try {
     $client = new Google_Client();
     $client->setAuthConfig(__DIR__ . '/Merchant_Center-c8fd21e1ec51.json'); 
     $client->addScope(Google_Service_ShoppingContent::CONTENT);
} catch (Google_Exception $e) {
    echo 'Error.';
}

$request_uri = 'https://mywebsite.com/googleShopping/';
$client->setRedirectUri($request_uri);

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

    if ($client->isAccessTokenExpired()) {
        unset($_SESSION['oauth_access_token']);
    }
} elseif (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $_SESSION['oauth_access_token'] = $token;
} else {
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
    exit;
}

$service = new Google_Service_ShoppingContent($client);
print_r($service->products->get(MERCHANT_ID, 'online:en:US:10081'));

Merchant_Center-c8fd21e1ec51.json file:

Merchant_Center-c8fd21e1ec51.json file

The error:

The error

Credentials:

Credentials

OAuth consent screen:

OAuth consent screen

Domain Verification:

Domain Verification

Service accounts:

Service accounts

Could someone tell me what could be happening? Thanks a lot.

Cava
  • 5,346
  • 4
  • 25
  • 41

2 Answers2

1

Click on this URL

Google mismatch

On Authorized redirect URIs set https://mywebsite.com/googleShopping/

If it's not showing any form you can follow this, Go to google console -> Select project then click on Credentials.

On there you can see the credentials for OAuth 2.0 client IDs. If not create one OAuth 2.0 (Create Credentials -> OAuth Client ID)

OAuth

Click on the edit button.

From that page, you can set the Authorized redirect URIs.

Redirect URI

Enter https://mywebsite.com/googleShopping/ as Authorized redirect URLs.

The redirect URL that you set here and code need to match.

Jijin P
  • 258
  • 1
  • 9
  • 21
-1

The solution:

<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/base.php'; // https://github.com/googleapis/google-api-php-client/blob/master/examples/templates/base.php

define('MERCHANT_ID', 'xxxxxxx');

$client = new Google_Client();

if ($credentials_file = checkServiceAccountCredentialsFile()) {
    try {
        // $credentials_file = service-account-credentials.json
        $client->setAuthConfig($credentials_file);
    } catch (Google_Exception $e) {
        echo 'Ocorreu um erro ao abrir o arquivo de configuração.';
    }
} else {
    echo missingServiceAccountDetailsWarning();
    exit;
}

$client->setApplicationName("Merchant Center");
$client->setScopes([Google_Service_ShoppingContent::CONTENT]);
$service = new Google_Service_ShoppingContent($client);
print_r($service->products->get(MERCHANT_ID, 'online:en:US:10081'));
Cava
  • 5,346
  • 4
  • 25
  • 41