我正在编写一个访问Google AnalyticsAPI的Ruby应用程序来提取一些实验信息 .
该应用通过以下功能使用Google服务帐户进行连接和身份验证:
def connect
...
@@client = Google::APIClient.new(:application_name => 'My Service App',
:application_version => '1.0.0')
key_file = Rails.root.join('config', 'privatekey.p12').to_s
key_secret = 'somesecret'
key = Google::APIClient::PKCS12.load_key(key_file, key_secret)
asserter = Google::APIClient::JWTAsserter.new(
SECRETS[:google_service_account_email],
['https://www.googleapis.com/auth/yt-analytics.readonly',
'https://www.googleapis.com/auth/analytics.readonly'
],
key
)
@@client.authorization = asserter.authorize()
...
end
...对API进行身份验证和发现,没有问题 .
针对YouTube Analytics API使用客户端可以正常运行 . 使用相同的帐户通过...访问Analytics API
response = @@client.execute({
# 'analytics is the API object retrieved via discover_api()
:api_method => analytics.management.experiments.list,
:parameters => {
'accountId' => 'AAAAAAAA',
'profileId' => 'PPPPPPPP',
'webPropertyId' => 'UA-WWWWWWWW-#'
}
})
结果出现403错误响应:
{"domain":"global","reason":"insufficientPermissions","message":"User does not have sufficient permissions for this account."}
关于授权,我已经仔细检查了帐户service@myapp.com:
-
Has full permissions to the Google Analytics web interface. 我使用service@myapp.com帐户登录,并且能够查看我尝试列出的相同实验 .
-
Has enabled the Analytics API. 在API控制台中,我在“服务”部分确认了Analytics API项目已切换为“开启” . (就像YouTube Analytics一样 . )
-
I am using the appropriate AccountID, ProfileID, and WebPropertyID values. 直接从Google Analytics网络界面复制 .
鉴于服务帐户可以访问至少一个API(YouTube Analytics),并且相关帐户(service@myapp.com)可以访问Google Analytics网络界面,特别是访问Analytics API的服务帐户似乎有问题 .
有任何想法吗?
类似主题:
8 回答
如果您是新手,只是为了让您知道需要3步骤程序
启用Google Analytics Reporting API,然后创建服务电子邮件帐户(OAuth客户端ID)
在Google控制台中添加并授予此服务电子邮件的权限
在Google Analytics跟踪中添加并授予此服务电子邮件帐户 - >用户管理
如果有人有更多视图,他应该使用视图ID .
即使在分析中将电子邮件添加到帐户级别后,我仍然遇到相同的权限问题,但以下建议有所帮助,但没有解决:
$ client-> setScopes(“ https ://www.googleapis.com/auth/plus.login”);
这不适合我,但这样做:
$ client-> setScopes(“ https ://www.googleapis.com/auth/analytics”);
https://现在需要进行身份验证 .
确保您提供服务帐户电子邮件(例如1234567890@developer.gserviceaccount.com)从GA视图读/写的权限 .
管理>查看>用户管理>“添加权限:”
Pick the right id!
在我的情况下,我使用正确的凭据(帐户ID,帐户密码 - > authorization_code - > access_token)并设置了正确的电子邮件权限,但我在 Admin > Account settings 页面上使用帐户ID,只是将ga:添加到前面 .
The id you actually need is the table id! (或's the one that worked for me at least since most people here are mentioning the account id, which didn'为我工作 . )你可以在这里找到一个:https://ga-dev-tools.appspot.com/account-explorer/
然后你可以查询为
我发现这个API总体上记录不清,而且用户界面还不清楚 . 但也许那只是我 .
如果在将开发人员电子邮件添加到分析用户后仍然看到此消息 .
在新的Google_Service_Analytics($ client)之前,您可能需要在对象中添加范围;
$ client-> setScopes(“https://www.googleapis.com/auth/plus.login”);
我整天都在努力解决这个问题!
确保您输入正确的table_id
我还必须转到开发人员控制台并启用API才能使其正常工作 . 转到the developer console并选择您的项目并启用您要使用的API .