php 401中的Google API OAuth需要登录

时间:2016-01-12 15:19:46

标签: php google-oauth google-api-php-client

我试图使用google 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阵列致命错误:未捕获的异常&#39; Google_Service_Exception&#39;   消息&#39; {&#34;错误&#34;:{&#34;错误&#34;:[{&#34;域&#34;:&#34;全球&#34;,&#34;原因&#34 ;:   &#34;必需&#34;,&#34;消息&#34;:&#34;需要登录&#34;,&#34; locationType&#34;:&#34;标题&#34;,   &#34; location&#34;:&#34;授权&#34; },&#34;代码&#34;:401,&#34;消息&#34;:&#34;登录   所需&#34; }&#39;在C:\ Program Files(x86)\ Apache Software中   基金会\ Apache24 \ htdocs中\谷歌的API的PHP客户端主机的\ src \谷歌\ HTTP \ REST.php:123   堆栈跟踪:#0 C:\ Program Files(x86)\ Apache Software   基金会\ Apache24 \ htdocs中\谷歌API的PHP客户端主机的\ src \谷歌\ HTTP \ REST.php(82):   Google_Http_REST :: decodeHttpResponse(对象(GuzzleHttp \ PSR7 \响应),   对象(GuzzleHttp \ Psr7 \ Request),&#39; Google_Service _...&#39;)#1 [内部   功能]:Google_Http_REST :: doExecute(对象(GuzzleHttp \ Client),   对象(GuzzleHttp \ Psr7 \ Request),&#39; Google_Service _...&#39;)#2 C:\ Program   文件(x86)\ Apache软件   基金会\ Apache24 \ htdocs中\谷歌的API的PHP客户端主机的\ src \谷歌\工作\ Runner.php(181):   call_user_func_array(Array,Array)#3 C:\ Program Files(x86)\ Apac in   C:\ Program Files(x86)\ Apache Software   基金会\ Apache24 \ htdocs中\谷歌的API的PHP客户端主机的\ src \谷歌\ HTTP \ REST.php   在第123行

感谢您的帮助

编辑:进一步的信息 我能够在$ token对象

中检测到这个错误
  

[error] =&gt; redirect_uri_mismatch [error_description] =&gt;错误的要求

     

块引用

1 个答案:

答案 0 :(得分:0)

/**
 * 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是动态的,带有代码参数