Adding new user via API - Last step HELP!

47 views
Skip to first unread message

Brian Logan

unread,
Jul 10, 2014, 3:47:05 PM7/10/14
to google-analytics...@googlegroups.com
I am working on a button that will allow our clients to give us access to their Google Analytics account without much hassle (instead of sending them the documents and having them attempt to follow/put in the wrong email address).  I have so far made the authentication with OAUTH2.0, and I have the accounts being listed so they can chose from a dropdown list which to give us access to.  The problem I am facing now is getting us to actually be added. They have the function mapped out, but it is not well documented yet. 

Here is where I am at:

function objectToArray($object){
$array=array();
foreach($object as $member=>$data)
{
$array[$member]=$data;
}
return $array;
}
session_start();
require_once('Google/Client.php');
require_once('Google/Service/Analytics.php');
require_once('Google/Service/Resource.php');
/** GOOGLE API INFORMATION **/
$client_id = '****';
$client_secret = '******';
$redirect_uri = '*****';
$developer_key = '****';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$authUrl = $client->createAuthUrl();
$service = new Google_Service_Analytics($client);
$user = new Google_Service_Analytics_EntityUserLink();
$analytics = new Google_Service_Analytics_ManagementAccountUserLinks_Resource();
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($_SESSION['access_token']) && $_SESSION['access_token']) {
 $client->setAccessToken($_SESSION['access_token']);
}
    if (!$client->getAccessToken()) {

$authUrl = $client->createAuthUrl();
echo "<h3>Step 1</h3>";
echo "<a class='login' href='$authUrl'>Connect to Google Analytics!</a>";

} else {

if($_GET['addaccount'] == 'true') {

            // THIS IS THE FUNCTION FOR INSERTING A NEW USER
            // BUT I DON'T KNOW HOW IT WORKS 100%
$analytics->insert($_POST['analytics_id'], $postBody, $optParams);

} else {

$accounts_convert = $service->management_accounts->listManagementAccounts();
$accounts = objectToArray($accounts_convert);
foreach($accounts AS $key => $val) {
if($val['kind'] == 'analytics#account') {
$data[$val['name']] = array('id' => $val['id']);
}
}
ksort($data);
echo '<h3>Step 2</h3>';
echo '<p>Choose an analytics account.</p>';
echo '<form method="post" action="?addaccount=true">';
echo '<select name="analytics_id" id="analytics_account">';
foreach($data AS $key => $val) {
echo '<option value="'.$val['id'].'">'.$key.'</option>';
}
echo '</select>';
echo '<input type="submit" value="Connect Analytics Account" />';
echo '</form>';

}
}

In the Analytics.php file, it gives this as a description for the function:
/**
* Adds a new user to the given account. (accountUserLinks.insert)
*
* @param string $accountId
* Account ID to create the user link for.
* @param Google_EntityUserLink $postBody
* @param array $optParams Optional parameters.
* @return Google_Service_Analytics_EntityUserLink
*/
public function insert($accountId, Google_Service_Analytics_EntityUserLink $postBody, $optParams = array())
{
   $params = array('accountId' => $accountId, 'postBody' => $postBody);
   $params = array_merge($params, $optParams);
   return $this->call('insert', array($params), "Google_Service_Analytics_EntityUserLink");
}

I am at a loss how to create the `$postBody`, and what the `$opParams` could be, (is this where you put in the permissions you would like, etc.). How do I make those two, and what could they be? (If I am doing it wrong, please forgive me, I am new to this, and like I said, no documentation.)

Pete

unread,
Jul 14, 2014, 11:33:44 AM7/14/14
to google-analytics...@googlegroups.com
You need to create a permissions object and set permissions, create a user object and set email, and then a user link object and set permissions and email using the previously created objects. For example:

// Setup Permissions
$permissions = new Google_Service_Analytics_EntityUserLinkPermissions();
$permissions->setLocal(array('READ_AND_ANALYZE'));

// Setup User
$user = new Google_Service_Analytics_UserRef();
$user->setEmail('g...@gmail.com');

// Setup user link (i.e. post body)
$el = new Google_Service_Analytics_EntityUserLink();
$el->setPermissions($permissions);
$el->setUserRef($user);

// Insert User
// $el is the post body
$response = $analytics->management_accountUserLinks->insert('123456', $el);
Reply all
Reply to author
Forward
0 new messages