dream3323 2016-01-12 15:19
浏览 46
已采纳

php 401中的Google API OAuth需要登录

I'm trying to use google api for shopping (merchants) and I'm stuck with this problem.

I'm not very familiar with php, but to me the code seems fine, all I wanna do is try to fetch items from my shop. I've set the client id and client secret in the developer console following the suggested steps by google, but still after logging I get the error.

Here the code I'm using:

<?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("
End");

////////////////////////
//$_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
", $product->getId(), $product->getTitle());
}

And I get the following error:

0 Array Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } ' in C:\Program Files (x86)\Apache Software Foundation\Apache24\htdocs\google-api-php-client-master\src\Google\Http\REST.php:123 Stack trace: #0 C:\Program Files (x86)\Apache Software Foundation\Apache24\htdocs\google-api-php-client-master\src\Google\Http\REST.php(82): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\Program Files (x86)\Apache Software 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)\Apac in C:\Program Files (x86)\Apache Software Foundation\Apache24\htdocs\google-api-php-client-master\src\Google\Http\REST.php on line 123

Thanks for any help

EDIT: further information I was able to detect this error inside the $token object

[error] => redirect_uri_mismatch [error_description] => Bad Request

Blockquote

  • 写回答

1条回答 默认 最新

  • douduan7295 2016-01-12 16:05
    关注
    /**
     * 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";
    

    the old uri was dynamic and with the code parameter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂