Refresh Token seemingly not refreshing - Ruby

52 views
Skip to first unread message

Kevin Taylor

unread,
Apr 25, 2016, 8:52:36 AM4/25/16
to AdWords API Forum
Hi,

So, we're having some issues with authentication in Ruby.

In our application, the user logs in and grants offline access. This seems to be working ok, we're getting both the access and refresh token and we're stashing them in the database as a hash that reads 

{access_token => 'XYZ',
refresh_token => 'ABC'}



We then have a backend process that is supposed to go through each job for that day and make the alterations the user wants using their stored credentials. This is where that task is falling down. Code at present is;

require 'adwords_api'

class Job
def initialize(job)
def initialize(job)
@id = job.id
@uniqueid = job.uniqueid
@campaignid = job.campaignid
@adgroupid = job.adgroupid
@authtoken = eval(job.token)
end



def get_report(report_type)
adwords = AdwordsApi::Api.new
jobtoken = @authtoken
token = adwords.authorize({
  :oauth2_token => jobtoken
  })
  api_version = :v201603
report_def_srv = adwords.service(:ReportDefinitionService, api_version)
fields = report_def_srv.get_report_fields(report)
end



(yeah, I know the eval on the jobtoken isn't secure) This works fine, and when I call get_report I get the data back.....so long as it's reasonably soon after the user granted authentication and the access token is still valid. Once that's no longer true, I get

AdwordsApi::V201603::ReportDefinitionService::ApiException: [AuthenticationError.OAUTH_TOKEN_INVALID @ ; trigger:'<null>']


I know there's supposed to be some sort of handshake where I give Google the refresh token and in exchange get the new access token, but the docs seem to suggest the Adwords Gem should handle that itself?

If anyone's got any ideas they'd be much appreciated.

Yin Niu

unread,
Apr 25, 2016, 11:14:45 AM4/25/16
to AdWords API Forum
Hi, 

Sounds like you are using OAuth web application flow instead of installed application flow. Please follow Using OAuth 2.0 for Web Server Applications guide. 

Thanks,
Yin, AdWords API Team. 
Reply all
Reply to author
Forward
0 new messages