【问题标题】:OAuth2 - Error 400: redirect_uri_mismatchOAuth2 - 错误 400:redirect_uri_mismatch
【发布时间】:2019-07-18 06:53:46
【问题描述】:

我拥有一个网站并希望将我的产品放在 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 文件:

错误:

凭据:

OAuth 同意屏幕:

域验证:

服务帐号:

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

【问题讨论】:

  • 请确保输入您的网址,https://mywebsite.com/googleShopping/ P.S - 请注意,redirect_uri 区分大小写
  • @AbhinavKinagi 没关系。

标签: php oauth-2.0 google-api


【解决方案1】:

点击这个网址

关于授权重定向 URI 设置 https://mywebsite.com/googleShopping/

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

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

点击编辑按钮。

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

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

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

【讨论】:

  • 这不起作用,因为我已经报告了这个问题。但感谢您的帮助。
  • 你在google控制台设置的重定向地址和脚本里设置的一样吗?
【解决方案2】:

解决办法:

<?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'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2021-09-06
    • 2021-06-22
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多