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

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

问题描述

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

我使用下面的代码,我的信誉很好,并且已经审查这篇文章......

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

session_start();

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

//访问https://cloud.google.com/console以生成您的
//客户端ID,客户端密钥,并注册您的重定向URI。
$ client-> setDeveloperKey('xxxxx');

$ service = new Google_Service_Analytics($ client);
try {
$ results = $ service-> management_experiments-> listManagementExperiments('xxxx','xxxx','xxxx');
} catch(apiServiceException $ e){
print'有一个Analytics API服务错误'。 $ e-> getCode()。 ':'。 $ E->的getMessage();

} catch(apiException $ e){
print'有一个通用的API错误'。 $ e-> getCode()。 ':'。 $ E->的getMessage();
}
echo'< pre>';
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. 创建Google Analytics服务对象

您已经完成了前两步,直接进入第3步创建服务对象。完成第1步后,您可以使用以下代码进行第2步。




以下是一个如何使用在PHP中的服务帐户 ServiceAccount



该示例项目用于PredictionService而不是谷歌分析服务。你需要稍微修改它。

  require_once'../../src/Google/Client.php'; 
require_once'../../src/Google/Service/Analytics.php';

//设置您的客户端ID,服务帐户名称和私钥的路径。
//有关获取这些密钥的更多信息,请访问:
// https://developers.google.com/console/help/#service_accounts

const 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示例);


//以PKCS 12格式加载密钥(您需要在创建服务帐户时从
// Google API控制台下载


$ client-> setAssertionCredentials(新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天全站免登陆