403尝试获取Google Analytics数据时权限不足错误 [英] 403 Insufficient permissions error while trying to get Google Analytics data
问题描述
我正在开发RoR应用程序,该应用程序允许用户使用Google帐户登录并从他的某个分析帐户下载/上传报告数据。
我使用 omniauth-google-oauth2 gem来授权Google帐户和 google-api-client 以与Google Analytics进行互动。
问题是,当我尝试使用 analytics.management.accounts.list 方法获取帐户列表时,将谷歌令牌作为授权参数之一传递,GA响应403 权限不足错误。
我想,我必须让用户在授权过程中授予阅读Google Analytics数据的权限。我怎样才能做到这一点?
我已经阅读了很多相关的问题,但没有找到解决我的确切问题的方法。
以下是代码:
require'google / api_client'
客户= Google :: APIClient.new(:application_name =>'你喜欢的东西',:application_version =>'1')
client.authorization.access_token ='HERE_GOES_TOKEN'
ga = client.discovered_api('analytics','v3')
client.execute(:api_method => ga.management.accounts.list)
解决了问题!我必须在OAuth配置中编写适当的作用域。
Rails.application.config.middleware.use OmniAuth :: Builder do
提供商:google_oauth2,PROVIDER_KEY,PROVIDER_SECRET,
{
:范围=> 电子邮件,个人资料,https://www.googleapis.com/auth/analytics,https://www.googleapis.com/auth/analytics.edit,https://www.googleapis.com/auth/analytics.manage .users,https://www.googleapis.com/auth/analytics.readonly,
}
结束
I'm developing RoR application, which allows user to login with google account and download/upload report data from one of his analytics account. I use omniauth-google-oauth2 gem for authorization with google account and google-api-client for interaction with Google Analytics. The problem is that when I try to get accounts list with analytics.management.accounts.list method, passing google token as one of authorization parameters, GA responds with 403 Insufficient permissions error.
I suppose, I have to make user to give permission for reading Analytics data in process of authorization. How can I do this?
I've read a lot of related questions, but didn't find solution to my exact problem.
Here is the code:
require 'google/api_client'
client = Google::APIClient.new(:application_name => 'something you like', :application_version => '1')
client.authorization.access_token = 'HERE_GOES_TOKEN'
ga = client.discovered_api('analytics', 'v3')
client.execute(:api_method => ga.management.accounts.list)
Solved the problem! I had to write proper scopes in OAuth configs.
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, PROVIDER_KEY, PROVIDER_SECRET,
{
:scope => "email, profile, https://www.googleapis.com/auth/analytics, https://www.googleapis.com/auth/analytics.edit, https://www.googleapis.com/auth/analytics.manage.users, https://www.googleapis.com/auth/analytics.readonly",
}
end
这篇关于403尝试获取Google Analytics数据时权限不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!