gpt4 book ai didi

oauth - Google Analytics API 在示例代码中返回 401

转载 作者:行者123 更新时间:2023-12-03 15:46:47 24 4
gpt4 key购买 nike

我正在尝试从谷歌分析的内容实验中检索数据......

我正在使用以下代码,我的信誉很好,并且已因这篇文章而受到审查...

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

session_start();

$client = new Google_Client();
$client->setApplicationName('Hello Analytics API Sample');

// Visit https://cloud.google.com/console to generate your
// client id, client secret, and to register your redirect uri.
$client->setDeveloperKey('xxxxx');

$service = new Google_Service_Analytics($client);
try {
$results = $service->management_experiments->listManagementExperiments('xxxx', 'xxxx', 'xxxx');
} catch (apiServiceException $e) {
print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage();
}
echo '<pre>';
print_r($results);
echo '</pre>';

我正在使用以下示例....

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#list

关于为什么我得到未经授权的 401 的任何想法?需要登录吗?

最佳答案

问题是您尚未授权访问您的数据。由于您说它只是您想要访问的您自己的数据,我建议您查看 service account .通过在 Google apis console 中设置服务帐户它将允许您访问自己的数据,而无需一直登录和验证代码。

检查以下链接。 阅读之前 你开始确保你做了所有这些。

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#service

  • 在 Google Developers Console 中注册您的应用程序
  • 授权访问 Google Analytics 数据。
  • 创建 Analytics 服务对象

  • 您已跳过前两个步骤,直接进入第 3 步创建服务对象。完成第 1 步后,您可以将以下代码用于第 2 步。

    下面是如何在 php ServiceAccount 中使用服务帐户的示例

    该示例项目适用于 PredictionService,而不是 google 分析服务。你需要稍微编辑一下。
    require_once '../../src/Google/Client.php'; 
    require_once '../../src/Google/Service/Analytics.php';

    // Set your client id, service account name, and the path to your private key.
    // For more information about obtaining these keys, visit:
    // https://developers.google.com/console/help/#service_accounts

    const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';
    const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME';


    // Make sure you keep your key.p12 file in a secure location, and isn't
    // readable by others.

    const KEY_FILE = '/super/secret/path/to/key.p12';


    $client = new Google_Client();
    $client->setApplicationName("Google Analytics Sample");


    // Load the key in PKCS 12 format (you need to download this from the
    // Google API Console when the service account was created.


    $client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME(Email),
    array('https://www.googleapis.com/auth/analytics.readonly'),
    file_get_contents(KEY_FILE))
    );


    $client->setClientId(CLIENT_ID);
    $service = new Google_Service_Analytics($client);

    现在你有 $service您可以在其余通话中使用。注意:我没有时间测试该代码,如果它不起作用,请告诉我,我会帮助您修复它。

    关于oauth - Google Analytics API 在示例代码中返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586590/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com