Google-api-ruby-client: ISSUES: Rails google analytics api error

Created on 24 Aug 2016  路  5Comments  路  Source: googleapis/google-api-ruby-client

Using the gem'google-api-client', require: 'google/apis/analytics_v3'

However, i am getting this error

ArgumentError in WelcomeController#analytics
Missing token endpoint URI.

(Error highlighted on Analytics method on @account_summaries)

Controller file

class WelcomeController < ApplicationController

    def redirect
      client = Signet::OAuth2::Client.new({
        client_id: 'APP ID',
        client_secret: 'APP SECRET',
        authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
        scope: Google::Apis::AnalyticsV3::AUTH_ANALYTICS_READONLY,
        redirect_uri: url_for(:action => :oauth2callback)
      })

      redirect_to client.authorization_uri.to_s
    end

    def oauth2callback
        client = Signet::OAuth2::Client.new({
        client_id: 'APP ID',
        client_secret: 'APP SECRET',
        token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
        redirect_uri: url_for(:action => :oauth2callback),
        code: params[:code]
      })

      response = client.fetch_access_token!

      session[:access_token] = response['access_token']

      redirect_to url_for(:action => :analytics)

    end

    def analytics
        client = Signet::OAuth2::Client.new(access_token: session[:access_token])

        access_token = Accesstoken.new 'client'

        service = Google::Apis::AnalyticsV3::AnalyticsService.new

        service.authorization = access_token

        @account_summaries = service.list_account_summaries(options: { authorization: client })


    end

end

I did follow an advice stated here - Missing token endpoint URI while using a valid access_token - but this still does not work.

class Accesstoken < ActiveRecord::Base
  attr_reader :token
  def initialize(token)
    @token = token
  end

  def apply!(headers)
    headers['Authorization'] = "Bearer #{@token}"
  end

end

Where is the issue??

Please note -

If in @account_summaries I put in authorization: access_token instead of client, it would then return Unauthorized. What is going on?

triage me

Most helpful comment

You can follow this excellent guide here. This would surely get things started - http://blog.jieck.com/server-side-integrates-google-analytics-api-with-ruby/

All 5 comments

The error means exactly that -- you're not configuring the oauth client with enough information.

I suggest using https://github.com/google/google-auth-library-ruby/ instead of signet directly.

@sqrrrl this worked a while ago, and now i get same issue. So something changed.

I'm not using OAuth2 for authentication (I'm using server credentials), but I recently updated the gem after a while and also got the Missing token endpoint URI. error. My solution was to use the #make_creds method.

I changed

Google::Auth::ServiceAccountCredentials.new(
  json_key_io: path_to_json_file,
  scope: ['https://www.googleapis.com/auth/analytics.readonly']
)

to

Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: path_to_json_file,
  scope: ['https://www.googleapis.com/auth/analytics.readonly']
)

and everything works fine now.

@arjunmenon Did you get this to work? I've had the exact same problem for 2 days now. Getting "Missing token endpoint URI". @23tux Would you mind showing an example with the rest of the code for that?

You can follow this excellent guide here. This would surely get things started - http://blog.jieck.com/server-side-integrates-google-analytics-api-with-ruby/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pacuna picture pacuna  路  5Comments

penakamaayo picture penakamaayo  路  3Comments

asheren picture asheren  路  5Comments

MattMencel picture MattMencel  路  3Comments

olegpanchenko picture olegpanchenko  路  3Comments