php 401中的Google API OAuth需要登录


Google API OAuth in php 401 Login Required

我正在尝试使用谷歌api购物(商家),但我遇到了这个问题。

我对php不是很熟悉,但对我来说,代码似乎很好,我只想尝试从我的商店里取东西。我已经按照谷歌建议的步骤在开发者控制台中设置了客户端id和客户端机密,但在登录后我仍然收到了错误。

这里是我使用的代码:

<?php
/*
 * Uses google API to make an authorized request trough a google dev console project.
 */
require_once __DIR__ . '/../../google-api-php-client-master/vendor/autoload.php';
//require_once 'Google/Client.php';
//require_once 'Google/Service/ShoppingContent.php';
require_once '../Config.php';
$redirect_uri = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //this is used during the auth process
//$merchantId = $_POST['merchantId'];
$merchantId = Config::MERCHANT_ID;
$service;
$client = Config::getGoogleClient();
$client->setRedirectUri($redirect_uri);
doOAuth($client);
echo "TOKEN : " + $client->getAccessToken() . " " . $_SESSION['oauth_access_token'];
//after authentication we can create a service object
$service = new Google_Service_ShoppingContent($client);
$products = $service->products->listProducts($merchantId);
$parameters = array();
while (!empty($products->getResources())) {
    foreach ($products->getResources() as $product) {
        printProduct($product);
    }
    if (!empty($products->getNextPageToken())) {
        break;
    }
    $parameters['pageToken'] = $products->nextPageToken;
    $products = $service->products->listProducts($merchantId, $parameters);
}
printf("'nEnd");
////////////////////////
//$_SESSION['oauth_access_token'] = null;
function doOAuth(&$client) {
    if (isset($_SESSION['oauth_access_token'])) {
        $client->setAccessToken($_SESSION['oauth_access_token']);
    } elseif (isset($_GET['code'])) {
//        $client->setAccessType("offline"); //IMPORTANT
        $token = $client->authenticate($_GET['code']);
        $_SESSION['oauth_access_token'] = $token;
    } else {
        header('Location: ' . $client->createAuthUrl());
        exit;
    }
}
function printProduct($product) {
    printf("%s %s'n", $product->getId(), $product->getTitle());
}

我得到以下错误:

0阵列致命错误:未捕获异常"Google_Service_exception"带有消息"{"error":{"errors":[{"domain":"global","reason":"必需","message":"需要登录","locationType":"header","location":"Authorization"}],"code":401,"message":"登录C:''Program Files(x86)''Apache Software中必需的"}}"Foundation''Apach24''htdocs''google-api-php-client-master''src''google''Http''REST.php:123堆栈跟踪:#0 C:''Program Files(x86)''Apache SoftwareFoundation''Apach24''htdocs''google api php client master''src''google''Http''REST.php(82):Google_Http_REST::decodeHttpResponse(对象(GuzzleHttp''Psr7''Response),对象(GuzzleHttp''Psr7''Request),"Google_Service_…")#1[内部function]:Google_Http_REST::doExecute(Object(GuzzleHttp''Client),对象(GuzzleHttp''Psr7''Request),"Google_Service_…")#2 C:''程序文件(x86)''Apache软件Foundation''Apache24''htdocs''google api php client master''src''google''Task''Runner.php(181):call_user_func_array(array,array)#3 C:''Program Files(x86)''ApacC: ''Program Files(x86)''Apache SoftwareFoundation''Apach24''htdocs''google api php client master''src''google''Http''REST.php在线123

感谢的帮助

编辑:进一步信息我能够在$token对象中检测到这个错误

[error]=>redirect_uri_mismatch[errordescription]=>错误请求

区块报价

/**
 * IMPORTANT: this url is used during both two phases of OAuth.
 * If a mistmatch of the redirect uri occours during the two phases, the login
 * fails. Must ignore the code (get) parameter in the second phase.
**/
$redirect_uri = "http://localhost/gshop/action/list.php";

旧的uri是动态的,代码参数