Google Analytics API 在示例代码中返回 401 [英] Google Analytics API Returning 401 On Example Code

查看:25
本文介绍了Google Analytics API 在示例代码中返回 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Google Analytics 的内容实验中检索数据...

我正在使用以下代码,我的信用很好,并且已经因为这篇文章被审查...

setApplicationName('Hello Analytics API Sample');//访问 https://cloud.google.com/console 生成您的//客户端 ID,客户端密码,并注册您的重定向 uri.$client->setDeveloperKey('xxxxx');$service = new Google_Service_Analytics($client);尝试 {$results = $service->management_experiments->listManagementExperiments('xxxx', 'xxxx', 'xxxx');} catch (apiServiceException $e) {打印有一个分析 API 服务错误".$e->getCode() .':' .$e->getMessage();} catch (apiException $e) {打印出现一般 API 错误".$e->getCode() .':' .$e->getMessage();}echo '

';print_r($results);echo '</pre>';

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

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

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

解决方案

问题是您尚未授权访问您的数据.由于您说它只是您想要访问的您自己的数据,因此我建议您查看 服务帐户.通过在 Google apis 控制台中设置服务帐户,您可以访问您的拥有自己的数据,而无需一直登录和验证代码.

检查以下链接.阅读之前,请确保您做到了所有这些.

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

  1. 在 Google Developers Console 中注册您的应用
  2. 授权访问 Google Analytics 数据.
  3. 创建一个 Analytics 服务对象

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

<小时>

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

该示例项目用于 PredictionService,而不是 google 分析服务.你需要稍微编辑一下.

require_once '../../src/Google/Client.php';require_once '../../src/Google/Service/Analytics.php';//设置您的客户端 ID、服务帐户名称和私钥的路径.//有关获取这些密钥的更多信息,请访问://https://developers.google.com/console/help/#service_accountsconst CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME';//确保您将 key.p12 文件保存在安全的位置,而不是//其他人可读.const KEY_FILE = '/super/secret/path/to/key.p12';$client = new Google_Client();$client->setApplicationName("Google Analytics Sample");//以 PKCS 12 格式加载密钥(您需要从//创建服务帐户时的 Google API 控制台.$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME(电子邮件),数组('https://www.googleapis.com/auth/analytics.readonly'),file_get_contents(KEY_FILE)));$client->setClientId(CLIENT_ID);$service = new Google_Service_Analytics($client);

现在您拥有了 $service,您可以将其用于其余的调用.注意:我没有时间测试该代码,如果它不起作用,请告诉我,我会帮助您修复它.

I am attempting to retrieve the data from our content experiments from google analytics...

I am using the following code, my creds are good, and have been censored for this post...

<?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>';

I am using the following example ....

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

Any ideas on why I am getting a 401 unauthorized? That a login is required?

解决方案

The problem is that you haven't Authorized access to your data yet. Since you say its only your own data you want to access i sugest you look into a service account. By setting up a service account in Google apis console it will allow you to access your own data with out needing to login and autenticate the code all the time.

Check the following link. Read though Before you begin make sure you do all that.

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

  1. Register your application in the Google Developers Console
  2. Authorize access to Google Analytics data.
  3. Create an Analytics service object

You have skiped the first two steps and gone directly to step 3 creating the service object. Once you have done step 1 you can use the following code for step 2.


Here is an example of how to use a service account in php ServiceAccount

That sample project is for the PredictionService not the google analytics service. You need to edit it slightly.

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);

Now you have $service that you can use with the rest of your calls. Note: I didnt have time to test that code let me know if it doesnt work and i will give you a hand in fixing it.

这篇关于Google Analytics API 在示例代码中返回 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆