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

Google API 401无效凭据

翟单弓
2023-03-14
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'lib/API/Google_Client.php';
require_once 'lib/API/contrib/Google_AnalyticsService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("APP name");

$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('mysecret');         
$client->setRedirectUri('http://example.com');
$client->setDeveloperKey('mykey');

$client->setScopes('https://www.googleapis.com/auth/analytics');

$service = new Google_AnalyticsService($client);

if (isset($_GET['logout'])) {
    session_destroy();
}

if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
//    $client->refreshToken('refresh-token');
    print_r(json_decode($_SESSION['token']));

    if ($client->isAccessTokenExpired()) {
        session_destroy();
        header('Location: http://example.com');
    }
}

if ($client->getAccessToken()) {
    $props = $service->management_webproperties->listManagementWebproperties("~all");
    print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";

    $accounts = $service->management_accounts->listManagementAccounts();
    print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";

    $segments = $service->management_segments->listManagementSegments();
    print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";

    $goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
    print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";

    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Object
(
    [access_token] => ya29.blablabla
    [token_type] => Bearer
    [expires_in] => 3600
    [refresh_token] => 1/Eblablabla
    [created] => 1413873721
)
    Google_ServiceException Object
(
    [errors:protected] => Array
        (
            [0] => Array
                (
                    [domain] => global
                    [reason] => authError
                    [message] => Invalid Credentials
                    [locationType] => header
                    [location] => Authorization
                )

        )

    [message:protected] => Error calling GET https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0: (401) Invalid Credentials
    [string:Exception:private] => 
    [code:protected] => 401
    [file:protected] => ...lib//API/io/Google_REST.php
    [line:protected] => 66
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => .../lib/API/io/Google_REST.php
                    [line] => 36
                    [function] => decodeHttpResponse
                    [class] => Google_REST
                    [type] => ::
                    [args] => Array
                        (
                            [0] => Google_HttpRequest Object
                                (
                                    [batchHeaders:Google_HttpRequest:private] => Array
                                        (
                                            [Content-Type] => application/http
                                            [Content-Transfer-Encoding] => binary
                                            [MIME-Version] => 1.0
                                            [Content-Length] => 
                                        )

                                    [url:protected] => https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0
                                    [requestMethod:protected] => GET
                                    [requestHeaders:protected] => Array
                                        (
                                            [authorization] => Bearer ya29.xxx
                                        )

                                    [postBody:protected] => 
                                    [userAgent:protected] => APP name google-api-php-client/0.6.5
                                    [responseHttpCode:protected] => 401
                                    [responseHeaders:protected] => Array
                                        (
                                            [vary] => Origin
Referer
X-Origin
                                            [www-authenticate] => Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token
                                            [content-type] => application/json; charset=UTF-8
                                            [date] => Tue, 21 Oct 2014 14:00:46 GMT
                                            [expires] => Tue, 21 Oct 2014 14:00:46 GMT
                                            [cache-control] => private, max-age=0
                                            [x-content-type-options] => nosniff
                                            [x-frame-options] => SAMEORIGIN
                                            [x-xss-protection] => 1; mode=block
                                            [server] => GSE
                                            [alternate-protocol] => 443:quic,p=0.01
                                            [transfer-encoding] => chunked
                                        )

                                    [responseBody:protected] => {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
                                    [accessKey] => 
                                )

                        )

                )

            [1] => Array
                (
                    [file] => .../lib/API/service/Google_ServiceResource.php
                    [line] => 186
                    [function] => execute
                    [class] => Google_REST
                    [type] => ::
                    [args] => Array
                        (
                            [0] => Google_HttpRequest Object
                                (
                                    [batchHeaders:Google_HttpRequest:private] => Array
                                        (
                                            [Content-Type] => application/http
                                            [Content-Transfer-Encoding] => binary
                                            [MIME-Version] => 1.0
                                            [Content-Length] => 
                                        )

                                    [url:protected] => https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties?key=AIzaSyCDOiczVHVPiXhynaTkLkQ-Jp2Kr8kngS0
                                    [requestMethod:protected] => GET
                                    [requestHeaders:protected] => Array
                                        (
                                            [authorization] => Bearer ya29.xxx
                                        )

                                    [postBody:protected] => 
                                    [userAgent:protected] => APP name google-api-php-client/0.6.5
                                    [responseHttpCode:protected] => 401
                                    [responseHeaders:protected] => Array
                                        (
                                            [vary] => Origin
Referer
X-Origin
                                            [www-authenticate] => Bearer realm="https://accounts.google.com/AuthSubRequest", error=invalid_token
                                            [content-type] => application/json; charset=UTF-8
                                            [date] => Tue, 21 Oct 2014 14:00:46 GMT
                                            [expires] => Tue, 21 Oct 2014 14:00:46 GMT
                                            [cache-control] => private, max-age=0
                                            [x-content-type-options] => nosniff
                                            [x-frame-options] => SAMEORIGIN
                                            [x-xss-protection] => 1; mode=block
                                            [server] => GSE
                                            [alternate-protocol] => 443:quic,p=0.01
                                            [transfer-encoding] => chunked
                                        )

                                    [responseBody:protected] => {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}}
                                    [accessKey] => 
                                )

                        )

                )

            [2] => Array
                (
                    [file] => .../lib/API/contrib/Google_AnalyticsService.php
                    [line] => 1010
                    [function] => __call
                    [class] => Google_ServiceResource
                    [type] => ->
                    [args] => Array
                        (
                            [0] => list
                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [accountId] => ~all
                                        )

                                )

                        )

                )

            [3] => Array
                (
                    [file] => .../oatuhCallback.php
                    [line] => 46
                    [function] => listManagementWebproperties
                    [class] => Google_ManagementWebpropertiesServiceResource
                    [type] => ->
                    [args] => Array
                        (
                            [0] => ~all
                        )

                )

        )

    [previous:Exception:private] => 
)

共有1个答案

高嘉熙
2023-03-14

我自己找到了答案。

当使用Google API时,您总是必须创建公共访问密钥,这给了您开发人员密钥。这是您正确的开发人员密钥。我遵循了一些论坛的指示,并得到了错误的方向,以获得我的开发人员密钥。

不幸的是,Googles OAuth集成说明中没有提到这一点。

 类似资料:
  • 我想使用脚本化方法(可能是通过)curl,从驱动器api访问一些简单的信息,例如创建日期。从本质上讲,我想在他们的Web界面中编写我可以做的事情:https://developers.google.com/drive/api/v3/reference/files/list。 我一直在使用curl命令,他们在上面的链接的查询中公开该命令: 我为此创建了一个API密钥(目前不受限制)。并使用此应用程序

  • 一些(但不是全部)Google帐户在尝试访问Google日历API时始终响应401,尽管Tokeninfo告诉我我正在使用的访问令牌具有适当的范围(请参阅下面的curl输出)。我可以使用刷新令牌成功获取新的访问令牌,但日历api继续到401。 有人知道为什么会发生这种情况吗?

  • 验证用户凭据时遇到问题。当我第一次给出正确的凭据时,一切正常,但首先给出无效的凭据,然后再给出正确的凭据,我会得到无效的凭据错误。我使用邮递员基本身份验证。 我的配置类: 和我的控制器类

  • 我有一个问题与Google plus访问令牌。它有时给我“无效凭据”错误消息,有时相同的标记显示为活动。 以下是步骤 是否有验证刷新令牌的方法?

  • 我正在使用类ldapconnection执行到LDAP服务器的绑定。在“正常”密码的情况下,它工作正常。但是,如果用户的密码包括ü、ä或ö,服务器将返回错误无效凭据。我假设是编码问题,但找不到任何设置。NET-类。因此,我查看了网络流量,我发现字母“ü'是用”FC“表示的十六进制。使用LDAP浏览器,“ü'由”C3BC“表示。 对此有什么想法吗?

  • 我是ldap新手,我尝试了一个我认为很简单的示例,用一个已经有人为测试设置的ldap实例来测试spring ldap模块。 有关我正在使用的ldap实例的详细信息可以在这里找到:http://blog.stuartlewis.com/2008/07/07/test-ldap-service/comment-page-3/ 我使用了ldap浏览器/管理工具(Softerra ldap Admin),