Google Analytics(分析)API-不一致的错误"insufficientPermissions"(403)
背景:
我有一个PHP代码,代表我的用户向Google Analytics(分析)API运行查询. 我正在使用OAuth2进行身份验证,并将用户的访问令牌存储在数据库中.
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.
我的代码确保不超过每个用户的配额(10个QPS),并且我在查询中使用"quotaUser"参数.
My code makes sure not to exceed the quota per user (10 QPS), and I'm using the "quotaUser" parameter in my queries.
问题:
大约50%的我对GA的查询均收到错误403("insufficientPermissions",用户对该配置文件没有足够的权限."). 奇怪的是,其他〜50%的人成功地从GA获得了结果.
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.
一些要点:
-
所有成功查询和不成功查询的唯一共同点是它们所在的批处理:我的代码正在发送一批"查询(一个接一个,查询之间的延迟很短)到GA的API,每批都是以太坊传递或失败,并显示403.
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.
值得一提的是,这不是View-ID \ Account-ID等问题,因为同一用户和视图的同一查询可能会通过或失败.
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);
Ido,您还能重现此问题吗?听起来很像您的访问令牌有问题.我将通过确保为所有批处理运行设置相同的访问令牌并验证令牌未过期(isAccessTokenExpired())来开始故障排除.
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()).