【发布时间】: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