我正在编写一个访问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:
拥有Google Analytics网络界面的完全权限.我使用service@myapp.com帐户登录,并且能够查看我尝试列出的相同实验.
已启用Analytics API.在API控制台中,我在"服务"部分确认了Analytics API项目已切换为"开启".(就像YouTube Analytics一样.)
我使用适当的AccountID,ProfileID和WebPropertyID值.直接从Google Analytics网络界面复制.
鉴于服务帐户可以访问至少一个API(YouTube Analytics),并且相关帐户(service@myapp.com)可以访问Google Analytics网络界面,特别是访问Analytics API的服务帐户似乎有问题.
有任何想法吗?
类似主题:
"权限不足"谷歌分析API服务帐户(注意:此错误与我的略有不同)
分析博客文章,查看评论部分的"权限"
Goose.. 53
确保您提供服务帐户电子邮件(例如1234567890@developer.gserviceaccount.com)从GA视图读/写的权限.
管理>查看>用户管理>"添加权限:"
选择正确的ID!
在我的情况下,我使用正确的凭据(帐户ID,帐户密码 - > authorization_code - > access_token)并设置了正确的电子邮件权限,但我在管理员>帐户设置 页面上使用帐户ID ,只需添加ga:到前方.
你真正需要的id是表id!(或者那个对我有用的,至少因为这里的大多数人都提到了账号,这对我不起作用.).你可以在这里找到一个:https://ga-dev-tools.appspot.com/account-explorer/
然后你可以查询为
service.get_ga_data(TABLE_ID,'2017-03-25','2017-03-25','ga:users,ga:pageviews')
我发现这个API总体上记录不清,而且用户界面还不清楚.但也许那只是我.
确保您提供服务帐户电子邮件(例如1234567890@developer.gserviceaccount.com)从GA视图读/写的权限.
管理>查看>用户管理>"添加权限:"