Wednesday, 10 July 2019

OAuth2 - Error 400: redirect_uri_mismatch

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.



from OAuth2 - Error 400: redirect_uri_mismatch

No comments:

Post a Comment