Re: Error in Service Account Example

296 views
Skip to first unread message

Eric

unread,
Jul 23, 2012, 9:22:21 AM7/23/12
to google-api...@googlegroups.com
Service accounts do not work with any service tied to a user account. You need to look at oauth using a refresh_token. Not at the office yet so I can't give you some example code


Eric Haskins
Sent from my Samsung smartphone on AT&T

Mohamed Chenouili <mchen...@gmail.com> wrote:
Hello,

I tried the service Account for GA with the new PHP version of google API, the one from trunk
and i get invalid_grant error
So i did generate a key for service account,
the service Account name was added in my Google analytics account as a user
and here is the code. I have no clue as for the reason for this error.
I tried it with prediction API and still the same error but as it is prediction request is costly
i don't know if it is the reason for the 'invalid_grant' error

session_start();
const CLIENT_ID = 'CLIENT_ID';
const SERVICE_ACCOUNT_NAME = 'SERVICE_ACCOUNT_NAME';
const KEY_FILE = './PATH_TO_KEYFILE.p12';

$client = new apiClient();
$client->setApplicationName("NAME_APPLICATION");

$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new apiAssertionCredentials(
  SERVICE_ACCOUNT_NAME,
  array('https://www.googleapis.com/auth/prediction'),
  $key)
);

$client->setClientId(CLIENT_ID);
$service = new apiAnalyticsService($client);
echo getFirstprofileId($service);

I get the error : invalid_grant i try to find another solution but no luck so far

Could you tell me what i am doing wrong ???
and if there is another solution to get the data from my GA account with automatic authentification
(how to generate an access token for each session,...) ???

Le jeudi 19 juillet 2012 12:31:42 UTC+2, robin a écrit :
Hi

who know that GA will support serviceaccount?

在 2012年4月13日星期五UTC+8上午3时58分45秒,andrew...@fcc.gov写道:
I downloaded the latest file today and am attempting to run the the
service account example here -
http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

I get this error -
Fatal error: Call to undefined method
apiClient::setAssertionCredentials() in Google_Analytics_API\index.php
on line 44

My index.php is the serviceAccount.php with include paths changed and
the key file, client id and service account name changed.

But that none of that seems to matter based on the error above.
Looking through the files pretty quickly that method doesn't exist
anywhere.

Any ideas? Am I missing some files? Did I get the wrong files? Again,
I downloaded the latest just today from here -
http://code.google.com/p/google-api-php-client/downloads/list

Any ideas???

Mohamed Chenouili

unread,
Jul 24, 2012, 2:49:54 AM7/24/12
to google-api...@googlegroups.com, Eric
Hello,

Thanks for the reply.

through an authentification, i retrieved the refresh token,
in a new connection, i take the refresh token to genarate a new access token
this get me the invalid grant error when i use google api as web application (client_id, secret, redirect uri, api_key).

$refreshToken = 'REFRESH_TOKEN';

if (isset($_SESSION['token']))
    $client->setAccessToken($_SESSION['token']);

if ($client->isAccessTokenExpired()){
    echo 'token expired';
    $token = $client->refreshToken($refreshToken);
    $_SESSION['token'] = $token;
    $client->setAccessToken($token);
} else {
    $analytics = new apiAnalyticsService($client);
    runMainDemo($analytics);
Reply all
Reply to author
Forward
0 new messages