Google Analytics API - inconsistent error "insufficientPermissions' (403)

旧街凉风 提交于 2019-12-12 03:17:15

问题


Backgroud:

I have a PHP code that runs queries to Google Analytics API on behalf of my users. I am using OAuth2 for authentication and storing the access-tokens of the users in my DB.

My code makes sure not to exceed the quota per user (10 QPS), and I'm using the "quotaUser" parameter in my queries.

The issue:

About 50% of my queries to GA are responded with error 403 ("insufficientPermissions", "User does not have sufficient permissions for this profile."). The strange this is, that the other ~50% are getting the results from GA successfully.

Some important points:

  • The only thing common to all the successful queries and common to the unsuccessful queries is the batch that they are in: my code is sending a "batches" of queries (one after one with very short delay between them) to GA's API and every batch is ether passing or failing with 403.

  • Adding \ removing permissions to the scope did not solved the issue.

  • It is worth mentioning that this is not a View-ID \ Account-ID issue etc. as the same query can pass or fail for the same user and view.

  • I saw a related unanswered issue here and couldn't find any other truly-related issues.

A snippet from my code:

//Create a Google Client
$client = new Google_Client();
$client->setAuthConfigFile($this->secretJson);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Oauth2::PLUS_LOGIN, Google_Service_Analytics::ANALYTICS);

// Set the access token on the client.
$client->setAccessToken($accessToken);

// Create an authorized analytics service object.
$this->analytics = new Google_Service_Analytics($client);

...

//Run the query
$results = $this->analytics->data_ga->get($id, $startDate, $endDate, $metrics, $opts);

回答1:


Ido, can you still reproduce this issue? It sounds very much like there is a problem with your access token. I would start troubleshooting by making sure you are setting the same access token for all batch runs and validating that the token is not expired (isAccessTokenExpired()).



来源:https://stackoverflow.com/questions/34995980/google-analytics-api-inconsistent-error-insufficientpermissions-403

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!