当前位置: 首页 > 知识库问答 >
问题:

需要登录php 401中的Google API OAuth

宇文鸿畴
2023-03-14

我正试图使用谷歌api购物(商家),我被这个问题卡住了。

我对php不是很熟悉,但对我来说,代码似乎很好,我想做的就是尝试从我的商店取货。我已经按照google建议的步骤在开发者控制台中设置了客户端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:“required”,“message:“Login required”,“locationType:“header”,“location:“Authorization”}],“code:”401,“message:“Login required”}在C:\Program Files(x86)中\Apache Software Foundation\Apache24\htdocs\google api php client master\src\google\Http\REST.php:123堆栈跟踪:#0 C:\Program Files(x86)\Apache Software Foundation\Apache24\htdocs\google api php client master\src\google\Http\REST.php(82):google_Http\u REST::decodeHttpResponse(对象(GuzzleHttp\Psr7\Response),对象(GuzzleHttp\Psr7\Request),“谷歌服务…”1[内部功能]:谷歌Http_REST::doExecute(Object(GuzzleHttp\Client),Object(GuzzleHttp\Psr7\Request),“谷歌服务…”2 C:\Program Files(x86)\Apache24\htdocs\Google api php Client master\src\Google\Task\Runner.php(181):调用用户函数数组(数组,数组)\3c:\Program Files(x86)\第123行C:\Program Files(x86)\Apache Software Foundation\Apache24\htdocs\google api php client master\src\google\Http\REST.php中的Apac

谢谢任何帮助

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

[错误]=

块引用

共有1个答案

杨安歌
2023-03-14
/**
 * 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是动态的,有代码参数

 类似资料:
  • 使用python代码从Goodreads获得书籍的描述,但是我的代码不允许我在开始实际搜索之前登录,我显然需要添加代码来使用我的用户名和pw进行登录,然后执行搜索。 现在发生的是,我首先得到了登录的弹出窗口,然后出现了错误: 请问有人能帮我一下吗?

  • http://172.16.7.203/sfAppServices/SF_UserLogin.svc/rest/login/ 在这个用户名和密码是它的一部分,我需要发布用户从edittext输入的用户名和密码,并得到如下json响应 如何使用改装获得json。我已使用asynctask成功完成此操作。但感觉很难通过改造来实现。我的问题是 哪个是基本网址? 在应该在post方法中公开的接口中。 如何

  • 我遵循了GitHub教程中的所有内容:https://help.github.com/articles/generating-ssh-keys 我在存储库的目录中执行了所有命令。我成功到达教程的末尾,并收到消息:“嗨,用户名!你已成功通过身份验证,但 GitHub 不提供外壳访问。 然而,当我尝试做推送之类的事情时,它仍然要求输入我的用户名和密码。

  • 我的服务注册得上eureka, 然后点击链接,正常应该显示如下信息: 但是,我的服务点击进去之后 提示需要登录 后端没有对/actuator/info进行拦截处理,不知道这个页面是怎么来的

  • 问题内容: 我想在我的Django编码网站中添加一些Ajax -niceness。 在我的Django代码中,我使用装饰器标记哪个视图需要认证。未经身份验证的用户单击时的默认行为是将其重定向到登录页面,然后传递目标页面。 我在某些网站上看到的,并且真正喜欢的是,当用户单击一个指向仅限于登录用户的位置的链接时,他/她得到了一个弹出窗口(通过JavaScript),而不是重定向到登录页面,而是询问他/

  • 在Android>Sample app>Trivial Drive中,当运行应用程序时,我在执行IAP时得到以下信息: 我在手机上用测试帐户(即我在开发人员控制台中指定的测试电子邮件地址)切换到了一个单独的帐户,并遵循了以下所有步骤: https://android.stackExchange.com/questions/20369/geting-authentication-is-require