Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to fetch the profiles from Google Analytics using Server to server OAuth2 #40

Closed
alokswain opened this issue Jun 4, 2015 · 5 comments

Comments

@alokswain
Copy link

Hi,
I am using the Signet gem to get OAuth2 access token for a service account. We are using Garb to fetch data from GA and display more logically for our consumers. We have updated Garb gem version 0.9.8.

I tried the following
Garb::Session.access_token = access_token
Garb::Management::Profile.all
and I encountered the following error:

NoMethodError: undefined method `get' for #<String:0xcd585ac>
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:94:in `oauth_user_request'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:41:in `send_request'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:21:in `response'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:13:in `parsed_response'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:17:in `entries'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/profile.rb:14:in `all'
    from (irb):16
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in `start'
    from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I think I am using it in a wrong way, I checked the docs and did not find anything related to fetching data by Authenticating by service accounts. I have verified from Google API playground the the access token I used is valid. Can you guide me as to what am I doing wrong ? Is it possible to do this via Garb (as I am not using Single User Login anymore as we need server to server interactions and using Service accounts is the only way.

Thanks,
Alok

@alokswain alokswain changed the title Unable to fetch the profiles from server Unable to fetch the profiles from Google Analytics using Server to server OAuth2 Jun 4, 2015
@YasuakiOmokawa
Copy link

I solved that by using gem below.

https://github.com/carnesmedia/google-oauth2-installed

but I choose "Installed application", so it's may not correct way for your
case.

2015-06-04 23:49 GMT+09:00 Alok Swain notifications@github.com:

Hi,
I am using the Signet gem to get OAuth2 access token for a service
account. We are using Garb to fetch data from GA and display more logically
for our consumers.

I tried the following
Garb::Session.access_token = access_token
Garb::Management::Profile.all
and I encountered the following error:

NoMethodError: undefined method get' for #<String:0xcd585ac> from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:94:inoauth_user_request'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/request/data.rb:41:in send_request' from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:21:inresponse'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:13:in parsed_response' from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/feed.rb:17:inentries'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/garb-0.9.8/lib/garb/management/profile.rb:14:in all' from (irb):16 from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:47:instart'
from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in start' from /home/alok/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.19/lib/rails/commands.rb:41:in<top (required)>'
from script/rails:6:in require' from script/rails:6:in

'

I think I am using it in a wrong way, I checked the docs and did not find
anything related to fetching data by Authenticating by service accounts. I
have verified from Google API playground the the access token I used is
valid. Can you guide me as to what am I doing wrong ? Is it possible to do
this via Garb (as I am not using Single User Login anymore as we need
server to server interactions and using Service accounts is the only way.

Thanks,
Alok


Reply to this email directly or view it on GitHub
#40.

┏◇─────────────────
┌─╂ 株式会社 先駈
■━┛ 代表取締役
│ 面川 泰明
│ TEL 03-6271-6858 FAX 03-6850-7117
│ Mail : omokawa@senk-inc.co.jp
□────────────────────

@alokswain
Copy link
Author

The problem is not with OAuth. When I use the access token obtained by using Signet, how do I use that access token in Garb to fetch data from GA ? I think the authentication process is already done as I have a valid Auth Token via Signet.

@rfernand
Copy link

rfernand commented Jun 6, 2015

I have the same problem as you @alokswain. Did you solve the issue?

@Sija
Copy link
Owner

Sija commented Jun 8, 2015

@alokswain is your access_token an instance of OAuth2::Client?

@alokswain
Copy link
Author

@Sija - Thats what I misunderstood. It works when Garb::Session.access_token is an instance of OAuth2::Client instead of a valid access_token. Thanks for the followup. I am closing the issue as I have resolved the problem and pasting the code I used below so that it might help someone in future:

key = Google::APIClient::KeyUtils.load_from_pkcs12("<p12_key_path>", "<p12key pwd>")
client = Google::APIClient.new(application_name: 'Service account demo', application_version: '0.0.1')

service_account = Google::APIClient::JWTAsserter.new(@ga_service_account, 'https://www.googleapis.com/auth/analytics.readonly', key)

client.authorization = service_account.authorize
oauth_client = OAuth2::Client.new("", "", {
  :authorize_url => 'https://accounts.google.com/o/oauth2/auth',
  :token_url => 'https://accounts.google.com/o/oauth2/token'
})

token = OAuth2::AccessToken.new(oauth_client, client.authorization.access_token, expires_in: 1.hour)
Garb::Session.access_token = token

profile = Garb::Management::Profile.all

Edit: Seems this can be directly done using Signet now (untested) and JWTAsserter is deprecated. Please refer Signet gem docs for more details on how to fetch the access token directly using Signet gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants