OAuth2-错误400:redirect_uri_mismatch

时间:2019-07-07 15:04:35

标签: php oauth-2.0 google-api

我拥有一个网站,并且希望将我的产品放置在Google购物上,我目前正在使用Google电子表格,但是我希望通过集成来做到这一点。自动导入我的产品。为此,我验证了我需要使用OAuth,但无法使其正常运行。我找不到错误可能在哪里。

代码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文件:

Merchant_Center-c8fd21e1ec51.json file

错误:

The error

凭据:

Credentials

OAuth同意屏幕:

OAuth consent screen

域验证:

Domain Verification

服务帐户:

Service accounts

有人可以告诉我会发生什么吗?非常感谢。

2 个答案:

答案 0 :(得分:0)

点击此URL

Google mismatch

在授权重定向URI上设置https://mywebsite.com/googleShopping/

如果未显示任何表格,您可以按照此步骤操作,转到Google控制台->选择项目,然后单击凭据。

在此您可以看到OAuth 2.0客户端ID的凭据。如果未创建,则创建一个OAuth 2.0(创建凭据-> OAuth客户端ID)

OAuth

单击编辑按钮。

在该页面上,您可以设置授权重定向URI。

Redirect URI

输入https://mywebsite.com/googleShopping/作为授权重定向URL。

您在此处设置的重定向URL和代码需要匹配。

答案 1 :(得分:0)

解决方案:

<?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'));
相关问题