【问题标题】:google Analytics API php APIKey 401谷歌分析 API php APIKey 401
【发布时间】:2014-08-28 23:00:10
【问题描述】:

我正在实现一个应该使用 Analytics DailyUpload API 的服务器端脚本

我在我的项目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"}}

我做错了吗?

【问题讨论】:

    标签: authentication google-analytics-api google-api-php-client


    【解决方案1】:

    感谢这个问题,我找到了方法。

    Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

    希望对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多