首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php APIKey 401

php APIKey 401
EN

Stack Overflow用户
提问于 2014-07-08 09:10:28
回答 1查看 3.6K关注 0票数 0

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

我在我的项目https://console.developers.google.com/project中添加了一个“服务器应用程序密钥”。

当我试图使用php客户端访问API时,总是会出现401错误。

代码语言:javascript
复制
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();
}

结果:

代码语言:javascript
复制
{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}

我做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-16 16:11:11

多亏了这个问题我找到了办法。

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

希望它能帮助别人

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24627922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档