Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am using rails, and i store the token only inside DB. then using a script i am setting up new client before calling execute, following is the code.</p> <pre><code>client = Google::APIClient.new(:application_name =&gt; 'my-app', :application_version =&gt; '1.0') client.authorization.scope = 'https://www.googleapis.com/auth/analytics.readonly' client.authorization.client_id = Settings.ga.app_key client.authorization.client_secret = Settings.ga.app_secret client.authorization.access_token = auth.token client.authorization.refresh_token = true client.authorization.update_token!({access_token: auth.token}) client.authorization.fetch_access_token! if client.authorization.refresh_token &amp;&amp; client.authorization.expired? client.authorization.fetch_access_token! end puts "Getting accounts list..." result = client.execute(:api_method =&gt; analytics.management.accounts.list) puts " ===========&gt; #{result.inspect}" items = JSON.parse(result.response.body)['items'] </code></pre> <p>But,it gives same error you are facing,</p> <pre><code> /signet-0.4.5/lib/signet/oauth_2/client.rb:875:in `fetch_access_token': Authorization failed. Server message: (Signet::AuthorizationError) { "error" : "invalid_grant" } from /signet-0.4.5/lib/signet/oauth_2/client.rb:888:in `fetch_access_token!' </code></pre> <p>Please suggest why it is not able to use the given token? I have used oauth2, so user is already authorized. Now i want to access the api and fetch the data...</p> <p>===================<strong>UPDATE</strong> ===================</p> <p>Ok, two issues were there,</p> <ol> <li><p>Permission is to be added to devise.rb,</p> <pre><code>config.omniauth :google_oauth2, Settings.ga.app_key,Settings.ga.app_secret,{ access_type: "offline", approval_prompt: "" , :scope =&gt; "userinfo.email, userinfo.profile, plus.me, analytics.readonly" </code></pre> <p>}</p></li> <li><p>refresh_token must be passed to the API call, otherwise its not able to authorize.</p></li> </ol> <p>I hope this helps to somebody, facing similar issue.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload