我设置了一个MC漏斗的例子,一切都很好!!:
- 从 git 下载了大师。
- 放一个帐户,它有效!
- 没关系!返回给我一些书籍样本的结果...
所以,我输入新代码来获取"mcf:",但收到" 401 未经授权">
问题:如何/在哪里授权查询"mcf:xxxxxxxx"指标和尺寸?
我使用的代码:
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
// $client->setApplicationName("Client_Library_Examples");
// $client->setScopes(['https://www.googleapis.com/auth/books']); //not sure here..
// $service = new Google_Service_Books($client);
$service = new Google_Service_Analytics($client); //not sure here..
$mcf = $service->data_mcf(
'ga:80456636',
'2015-02-22',
'2015-02-23',
'mcf:totalConversionValue',
array(
'dimensions' => 'mcf:source,mcf:medium'
)
);
echo"<pre>";
var_dump($mcf);
echo"</pre>";
首先,
您注释掉了$client的所有代码。 Google 图书是一个公共 API,这意味着您可以使用公共 API 密钥进行身份验证。 Google Analytics数据是私人数据,这意味着您需要使用Oauth2或服务帐户。
服务帐户示例
require_once 'Google/autoload.php';
session_start();
/************************************************
The following 3 values an befound in the setting
for the application you created on Google
Developers console. Developers console.
The Key file should be placed in a location
that is not accessable from the web. outside of
web root. web root.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = '[Your client id]';
$Email_address = '[YOur Service account email address Address]';
$key_file_location = '[Locatkon of key file]';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/analytics.readonly";
$cred = new Google_Auth_AssertionCredentials($Email_address,
array($scopes),
$key);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Analytics($client);
$mcf = $service->data_mcf->get('ga:80456636',
'2015-02-22',
'2015-02-23',
'mcf:totalConversionValue',
array(
'dimensions' => 'mcf:source,mcf:medium'
)
);
echo"<pre>";
var_dump($mcf);
echo"</pre>";