0

내가 분석 DailyUpload의 APIGoogle 웹 로그 분석 API PHP는 apiKey에 401

내 프로젝트 https://console.developers.google.com/project에서 "서버 응용 프로그램을위한 키"를 추가를 사용하기로 서버 측 스크립트를 구현하고 있습니다.

PHP 클라이언트에서 API에 액세스하려고 할 때마다 항상 401 오류가 발생합니다.

set_include_path("../src/" . PATH_SEPARATOR . get_include_path()); 
require_once 'Google/Client.php'; 
require_once 'Google/Service/Analytics.php'; 

$client = new Google_Client(); 
$client->setApplicationName("AutoDailyUpload"); 
$apiKey = "MY_API_KEY"; 
$client->setDeveloperKey($apiKey); 

$analytics = new Google_Service_Analytics($client); 
try { 
    $accounts = $analytics->management_accounts->listManagementAccounts(); 

    if (count($accounts->getItems()) > 0) { 
     $items = $accounts->getItems(); 
     $firstAccountId = $items[0]->getId(); 

     $webproperties = $analytics->management_webproperties->listManagementWebproperties($firstAccountId); 

     if (count($webproperties->getItems()) > 0) { 
      $items = $webproperties->getItems(); 
      $firstWebpropertyId = $items[0]->getId(); 

      $profiles = $analytics->management_profiles->listManagementProfiles($firstAccountId, $firstWebpropertyId); 

      if (count($profiles->getItems()) > 0) { 
       $items = $profiles->getItems(); 
       echo $items[0]->getId(); 

      } else { 
       throw new Exception('No views (profiles) found for this user.'); 
      } 
     } else { 
      throw new Exception('No webproperties found for this user.'); 
     } 
    } else { 
     throw new Exception('No accounts found for this user.'); 
    } 
} catch (Exception $e) { 
    echo 'Error : ' . $e->getMessage(); 
} 

결과 :

{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}} 

내가 뭔가 잘못하고있는 중이 야?

답변