且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

(403)您的项目无权访问此功能

更新时间:2022-11-25 22:54:06

写操作仍处于测试阶段。在您可以访问此功能之前,您需要请求访问测试版。


在Management API中编写操作(例如,创建,更新,删除,Web资源,View(Profile)和目标资源是
,现在可用作开发人员预览的有限测试版。如果您对
感兴趣使用这些功能,请请求访问测试版

更新:需要大约3周的时间才能获得批准。当您获得批准后,您会收到Google发送的一封电子邮件,邀请您加入Beta测试小组。

Been following the documentation on how to insert webproperties so that i can create tracking codes dynamically. The objective is to move our analytics over in Google Analytics & automatically creating the customers website on their own google account under analytics. After what little i could find online it seems like this feature might be whitelisted. So I'm making this question to figure out weather or not this is the case. Documentation is hard to figure out, cause it doesn't tell you what fields are required, what the fields mean etc. Also seems like the documentation is outdated for the PHP library. Had to change alot of the example code class names & method names to get it "working".

Here is the code snippet that i use to test this feature out.

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

session_start();

$client = new Google_Client();

$client->setClientId('xxxxx');
$client->setClientSecret('xxxxxx');
$client->setRedirectUri('xxxxxxxx');
$client->setDeveloperKey('xxxxxxx');
$client->setScopes(
    array(
          'https://www.googleapis.com/auth/analytics.readonly',
          'https://www.googleapis.com/auth/analytics',
          'https://www.googleapis.com/auth/userinfo.profile',
          'https://www.googleapis.com/auth/analytics.edit',
          'https://www.googleapis.com/auth/analytics.manage.users'
    )
);

$oauth2 = new Google_Service_Oauth2($client);

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
}

if (!$client->getAccessToken()) {
  $authUrl = $client->createAuthUrl();

  print "<a class='login' href='$authUrl'>Connect Me!</a>";

} else {
    $analytics = new Google_Service_Analytics($client);

    try {
        $user = $oauth2->userinfo->get();

        $permission = new Google_Service_Analytics_WebpropertyPermissions();
        $permission->setEffective(array('EDIT', 'VIEW'));

        $trackingObject = new Google_Service_Analytics_Webproperty();
        $trackingObject->setAccountId($user['id']);
        //$trackingObject->setDefaultProfileId($user['id']);
        $trackingObject->setId('UA-xxxxx-1');
        $trackingObject->setPermissions($permission);
        $trackingObject->setIndustryVertical('INTERNET_AND_TELECOM');
        $trackingObject->setLevel('STANDARD');
        $trackingObject->setName('xxxxxx');
        $trackingObject->setWebsiteUrl('xxxxxx');

        $analytics->management_webproperties->insert($user['id'], $trackingObject);
        $accounts = $analytics->management_accounts->listManagementAccounts();
        echo '<pre>'; print_r($user); echo '</pre>';
        echo '<pre>'; print_r($accounts); echo '</pre>';
        die();

  } catch (apiServiceException $e) {
    // Error from the API.
    print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();

  } catch (Exception $e) {
    print 'There was a general error : ' . $e->getMessage();
  }
}

The exception i get is what title contains.

Write operations are still in beta. Before you can access this feature you need to request access to the beta.

Write operations in the Management API (e.g. create, update, delete, patch) for Web Property, View (Profile), and Goal resources is currently available as a developer preview in limited beta. If you're interested in using these features, request access to the beta.

Update: It takes around 3 weeks to get approved. When you are approved you will receive an email from Google inviting you to join the beta test group.

推荐文章