Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google analytics 如何使用omniauth_google_oauth2的令牌连接garb?_Google Analytics_Omniauth_Garb Gem - Fatal编程技术网

Google analytics 如何使用omniauth_google_oauth2的令牌连接garb?

Google analytics 如何使用omniauth_google_oauth2的令牌连接garb?,google-analytics,omniauth,garb-gem,Google Analytics,Omniauth,Garb Gem,我需要连接到谷歌分析。我正在使用omniauth\u google\u oauth2对应用程序的用户进行身份验证,这给了我一个令牌。问题是我需要连接到该用户的Google Analytics帐户。要将GA连接到我的应用程序,我使用的是gem,它只有两种身份验证方法;用户名/密码和OAuth令牌。当我使用omniauth\u google\u oauth2提供的令牌时,它不起作用 如何仅使用从omniauth_google_oauth2身份验证中获得的oauth_令牌来创建此新令牌?我认为您遇到的

我需要连接到谷歌分析。我正在使用
omniauth\u google\u oauth2
对应用程序的用户进行身份验证,这给了我一个令牌。问题是我需要连接到该用户的Google Analytics帐户。要将GA连接到我的应用程序,我使用的是gem,它只有两种身份验证方法;用户名/密码和OAuth令牌。当我使用omniauth\u google\u oauth2提供的令牌时,它不起作用


如何仅使用从omniauth_google_oauth2身份验证中获得的oauth_令牌来创建此新令牌?

我认为您遇到的问题是,
garb
将仅使用oauth 1(或用户名/密码组合)对用户进行身份验证,而
omniauth_google_oauth2
显然是oauth 2

我找到的唯一解决方案是使用Google不推荐的OAuth 1实现,如下所示

档案:

gem 'omniauth-google', :git => 'git://github.com/davidkpham/omniauth-google.git'
# This fork relaxes dependencies on omniauth itself
初始值设定项(用于谷歌分析访问):

在回调中,存储一些传回的内容:

auth = request.env["omniauth.auth"]
session[:google_token] = auth.credentials.token
session[:google_secret] = auth.credentials.secret
然后为
garb
构造一个AccessToken:

if session[:google_token] and session[:google_secret]
  consumer = OAuth::Consumer.new('XXXXXXXXXXXX.apps.googleusercontent.com', 'YOUR_SECRET', {
    :site => 'https://www.google.com',
    :request_token_path => '/accounts/OAuthGetRequestToken',
    :access_token_path => '/accounts/OAuthGetAccessToken',
    :authorize_path => '/accounts/OAuthAuthorizeToken'
  })
  garbsession = Garb::Session.new
  garbsession.access_token = OAuth::AccessToken.new(consumer, session[:google_token], session[:google_secret])
  # Once we have an OAuth::AccessToken constructed, do fun stuff with it
  ga_id = "UA-XXXXXXX-X"
  profile = Garb::Management::Profile.all(garbsession).detect {|p| p.web_property_id == ga_id}
  ga_monthly = GoogleAnalyticsDate.results(profile, :start_date => (Date.today - 30), :end_date => Date.today, :sort => :date)
  puts ga_monthly
end

我知道我在这件事上迟到了,但我解决了一个类似的问题。您不能将omniauth_google_oauth2与garb一起使用,除非您使用支持oauth2的garb分叉。有一个是由Sija很好地维护的。但是,您需要使用oauth2客户机对象来创建具有此fork的会话。您可以使用omniauth_google_oauth2设置用户的配置文件,并确保为用户保存刷新令牌,然后当您想要使用garb获取分析数据时,使用oauth2刷新令牌,然后将该对象传递到garb会话以提取用户数据。以下是将omniauth的refresh_令牌存储在某处后的示例:

client = OAuth2::Client.new YOURGOOGLEAPIKEY, YOURGOOGLEAPISECRET,
                                {
                                  :site => 'https://accounts.google.com',
                                  :authorize_url => "/o/oauth2/auth",
                                  :token_url => "/o/oauth2/token",
                                }
response = OAuth2::AccessToken.from_hash(client, :refresh_token => omniauth_refresh_token).refresh!
Garb::Session.access_token = response

谢谢你的回答,这节省了我很多时间。如果有人在配置方面有问题,我有一个示例应用程序,它部分基于这个答案:这仍然有效吗?我正在使用Signet为服务帐户获取OAuth2访问令牌(因为我需要服务器到服务器),然后创建一个新的Garb::Session对象,并以与应答相同的方式为其分配访问令牌。我在
Garb::Management::Profile.all(garbsession)
中遇到一个错误,我希望您能对此进行一点扩展。我认为带Sija服装叉的OAuth2是解决这个问题的正确方法。我似乎做不到这一点。你对哪一部分有困难?是从omniauth\u google\u oauth2获取刷新令牌、刷新令牌、设置sija的fork,还是使用刷新响应来启动garb会话?我想出来了。。。我想我只是在使用
irb
时遇到了问题。您的代码片段非常正确。Sija的fork现在似乎是首选版本。我可以确认它与omniauth_google_oauth2和garb.Topher一起工作非常好-谢谢你的评论。。。几个月前,当我开始工作时,这对我很有帮助。最近,虽然我的应用程序停止工作,但给出了一个错误代码:invalid_grant:{“error”:“invalid_grant”}您有过这样的经历吗?对可能发生的事情有什么见解吗?
client = OAuth2::Client.new YOURGOOGLEAPIKEY, YOURGOOGLEAPISECRET,
                                {
                                  :site => 'https://accounts.google.com',
                                  :authorize_url => "/o/oauth2/auth",
                                  :token_url => "/o/oauth2/token",
                                }
response = OAuth2::AccessToken.from_hash(client, :refresh_token => omniauth_refresh_token).refresh!
Garb::Session.access_token = response