Google API PHP Client을 사용하여 Google 웹 로그 분석에 액세스하는 방법을 알아 내려고 노력하고 있습니다. 구체적으로 다른 캠페인에서 upload cost data (으)로 보내고 싶습니다. 내 코드는 GA에서 정보를 얻기 위해 작동하지만 프로필 트래픽 데이터를 볼 수는 있지만 업로드 방법을 알 수는 없습니다. 데이터를 요청하는Google Analytics API 401 유효하지 않은 자격증 명
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("GA-Tester");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://www.site.com/indext.php');
$client->setDeveloperKey('xxx');
$analyticsService = new Google_AnalyticsService($client);
$dailyUploads = $analyticsService->management_dailyUploads;
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
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']);
}
if (!$client->getAccessToken()) {
header ('Location:http://www.site.com/indext.php');
} else {...
이 코드가하는 일 :
는 여기가 인증을 위해 사용하고 코드입니다. 계정 목록, 프로필, 특정 프로필의 트래픽 데이터 다운로드 등을 할 수 있습니다. 오류나 문제는 없습니다.비용 데이터가 포함 된 CSV 파일을 업로드하려고하면 '401 Invalid Credentials'오류가 발생합니다. 파일을 전송하는 코드는 다음과 같습니다.
$send = $dailyUploads->upload(
$accountId,
$webPropertyId,
$customDataSourceId,
$start_date,
1,
'cost',
array(
"reset" => true,
"data" => $cont,
"mimeType" => "application/octet-stream",
"uploadType" => "media"));
내가 전달하는 모든 변수를 두 번 확인했는데 올바른 정보를 전송하고 있습니다.
여기 내 질문이 있습니다. GET
요청이 모두 문제없이 작동하면 내 POST
요청이 오류를 발생시키는 이유는 무엇입니까? 이 코드가 내 코드 또는 API console에있는 설정인지 여부를 알 수 없습니다. 누구든지 올바른 방향으로 나를 조종 할 수 있습니까?
편집 : 다음은 생성 된 오류 코드입니다 (식별 가능한 비트 제외).
Fatal error: Uncaught exception 'Google_ServiceException' with message
'Error calling POST https://www.googleapis.com/upload/analytics/v3/man
agement/accounts/1234567/webproperties/UA-1234567-1/customDataSources/
xXxXxX/dailyUploads/2013-01-17/uploads?appendNumber=1&type=cost&reset=
true&uploadType=media&key=xXxXxX: (401) Invalid Credentials' in /../pub
lic_html/proj/google-api-php-client/src/io/Google_REST.php:66 Stack tra
ce: #0 /../public_html/proj/google-api-php-client/src/io/Google_REST.ph
p(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1/
../public_html/proj/google-api-php-client/src/service/Google_ServiceRes
ource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /..
/public_html/proj/google-api-php-client/src/contrib/Google_AnalyticsSer
vice.php(82): Google_ServiceResource->__call('upload', Array) #3 /../pu
blic_html/proj/dailyUpload_send.php(60): Google_ManagementDailyUploadsS
erviceResource->upload('1234567', 'UA-1234567-1', 'xXxXxXxXxXxXxXx...',
' in /../public_html/proj/google-api-php-client/src/io/Google_REST.php
on line 66
이걸 보셨습니까? https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtDailyUploadGuide#upload –
예, 내 요청은이 코드를 기반으로합니다. PHP 라이브러리를 사용하여 동일하게 보이지 않지만 내 코드에는 이러한 각 요소가 포함되어 있습니다. – Lenwood
승인 범위를 https://www.googleapis.com/auth/analytics에서 승인 했습니까? –