4
Oauth2을 사용하여 웹 로그 분석 데이터에 액세스하려고합니다. 인증 코드를 가져 와서 승인 토큰으로 변경합니다. 하지만 애널리틱스 API에서 데이터를 가져 오려고하면 401 잘못된 자격 오류가 발생합니다.Google API 401 유효하지 않은 자격증 명
내가 사용하는 코드는 PHP client library에서입니다 :
<?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>";
}
?>
가 내 자격 증명을 tripplechecked.
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] =>
)
내가 잘못 뭐하는 거지 :
Object
(
[access_token] => ya29.blablabla
[token_type] => Bearer
[expires_in] => 3600
[refresh_token] => 1/Eblablabla
[created] => 1413873721
)
내가 GA 계정에서 웹 속성을 얻으려고
내가 401 오류가 발생합니다 : 내 액세스 토큰은 다음과 같이 보이는?
어떻게 할 수 있는지 배웠던 포럼에 대한 액세스 권한을 제공해 주시겠습니까? – tixastronauta
더 자세한 지침을 제공해 주시겠습니까? –