Ruby and OAuth

422 views
Skip to first unread message

Thomas

unread,
May 30, 2012, 5:41:43 AM5/30/12
to adwor...@googlegroups.com
Hello,

I'm trying to work out how to use OAuth with the Google Adwords API. Sofar I didn't have much luck. While using ClientLogin is working perfect, OAuth will always throw "AuthenticationError.OAUTH_TOKEN_INVALID".

I tried my best to stay as close to the Ruby-Example in v201109_01 as possible, but still no luck.

What works:

1) Making a request against the API with OAuth returns an AdsCommon::Errors::OAuthVerificationRequired Error.
2) Using the provided URL to redirect the user to the authentication page (why not provide the redirection URL in the ruby example?)

Now it stops working: I set the verification code like so (like in the example): credential_handler.set_credential(:oauth_verification_code, options[:oauth_verification_code]).
Now doing a new request results in AuthenticationError.OAUTH_TOKEN_INVALID.

I honestly couldn't get it to work. My questions:

1) Could it have something todo with the account I'm using to allow access to Adwords? I'm using an MCC-Account with administration rights.
2) Why is the ruby implementation so different from phyton or php? (UpgradeAuthToken?)

I'm really at a loss here.

Best regards
  Thomas

Danial Klimkin

unread,
May 30, 2012, 6:45:26 AM5/30/12
to adwor...@googlegroups.com
Hello Thomas,


I am the library author and will be happy to help you to get it working.

As for the example, it does not provide redirect URL as it doesn't make sense for a console application. Does the example code work if you make no changes and run it as-is, passing verification code?

We plan to release a Rails example to better demonstrate the OAuth method with redirect URL soon.


-Danial, AdWords API Team.

Thomas

unread,
May 30, 2012, 8:48:44 AM5/30/12
to adwor...@googlegroups.com
Hello Danial,

thx for the fast reply.

I got the example working (which I should have done from the beginning).

The problem is, that my programming-workflow is different from the example.

Example (stateful): 
1) make request (with adwords-obj, and generate consumer)
2) get token
3) make new request (reuse adwords-obj with consumer)

My App (stateless):
1) make request (new adwords-obj)
2) redirect user to Auth-Page
3) create new adwords-obj (new consumer) based on data from a db
4) make request with verification token set

Now, the problem seems to me, that I don't know which tokens I need to save and where to get them from. I don't want the User to authenticate each request. This problem is addressed in the other libraries (UpgradeOAuthToken), but as far as I can see, not in the ruby library.

Also, I've noticed (which is the most likely culprit), that the cosumer-object is created during/after a request is made. That seems to be the underlying assumption in the ruby-example. But I'm not sure.

best regards
  Thomas

Danial Klimkin

unread,
May 30, 2012, 10:51:34 AM5/30/12
to adwor...@googlegroups.com
Hello Thomas,


You can request an authorization and get token this way (in the callback):

token = api.authorize({:oauth_request_token => request_token, :oauth_verification_code => params[:oauth_verifier]})
session[:token] = session[:token]


Here is how I re-create the API object from the session:

api = AdwordsApi::Api.new(config_filename)
token = session[:token]
# If we have an OAuth token in session we use the credentials from it.
if token
  credentials = api.credential_handler()
  credentials.set_credential(:oauth_token, token.token)
  credentials.set_credential(:oauth_token_secret, token.secret)
  # You may want to restore the CID as well
  credentials.set_credential(:client_customer_id, selected_account)
end

Depending on how you store the session you may want to store just 2 strings instead of the full token object.



-Danial, AdWords API Team.


On Wednesday, May 30, 2012 1:41:43 PM UTC+4, Thomas wrote:

Thomas

unread,
May 30, 2012, 12:17:43 PM5/30/12
to adwor...@googlegroups.com
Hello Danial,

many many thanks. api.authorize was the missing link. This is great!

Best regards
  Thomas

yoshi

unread,
Aug 7, 2012, 5:15:51 AM8/7/12
to adwor...@googlegroups.com
Where do you get the request_token object from?

Danial Klimkin

unread,
Aug 8, 2012, 11:20:10 AM8/8/12
to adwor...@googlegroups.com
Hello yoshi,


The library provides it as the exception field, see:


I recommend to use the OAuth2 instead though. OAuth1.0a is deprecated and should not be used for the new projects.


-Danial, AdWords API Team.

Danial Klimkin

unread,
Mar 21, 2013, 3:24:16 AM3/21/13
to adwor...@googlegroups.com
Hello Hannes,


There is no such guide, but the difference for the library users should be minimal.

authorize() will return you a hash which needs to be stored. The permanent token is called "refresh_token", it is used to obtain a fresh access_token when required.

Please see the AdWords on Rails example for more details:



-Danial, AdWords API Team.


On Monday, March 18, 2013 6:59:48 PM UTC+4, johanne...@chads.io wrote:
Hello Danial,

is there a guide to convert from OAuth 1.0a to OAuth2? Specifically, is it still possible to use 'api.authorize' to receive a session token that is indefinitely valid?

Best regards
  Hannes

johanne...@chads.io

unread,
Mar 22, 2013, 9:26:05 AM3/22/13
to adwor...@googlegroups.com
Thank you Danial.

Your help is always very much appreciated. Keep it!

Best regards
  Hannes

johanne...@chads.io

unread,
Mar 23, 2013, 2:20:38 PM3/23/13
to adwor...@googlegroups.com
One quick last question.

When I'm reusing a token generated by 
api.authorize(
{
  :oauth2_callback => login_callback_url,
  :oauth2_verification_code => params[:code]
} it seems as if the token does not expire. I have waited 6 hours and I can still reuse the same token for API calls. Is it possible that the Oauth2-Api itself requests a new token and uses this instead of the provided one, if the token is expired? Best regards Johannes

johanne...@chads.io

unread,
Mar 23, 2013, 2:37:43 PM3/23/13
to adwor...@googlegroups.com
:) Found it. It indeed does refresh the token automatically. This is pretty awesome!

Christopher Sell

unread,
May 27, 2013, 4:02:21 PM5/27/13
to adwor...@googlegroups.com
Hey Johanne so if I store the access token hash and use it when creating api instances, it will use the refresh token to generate new access tokens on its own?

Can you verify this? Where did you find this in the gem? Can you point me to a section. I'd like to know how this works as I have done it manually with the other APIs

Danial Klimkin

unread,
May 28, 2013, 5:04:15 AM5/28/13
to adwor...@googlegroups.com
Hello Christopher,


If you initialize the library instance with OAuth token (with referesh_token in it), the library will auto-refresh it based on the expires_at value.

You can find the gem on rubygems (gem install google-adwords-api) or on the code site:



-Danial, AdWords API Team.

Matthew Ledom

unread,
Jul 19, 2013, 3:55:53 PM7/19/13
to adwor...@googlegroups.com
What is the token.secret? Is that the same thing as the client secret?

Danial Klimkin

unread,
Jul 24, 2013, 5:53:48 AM7/24/13
to adwor...@googlegroups.com
Hello Matthew,


This is a thread from 2012 and things have changed a bit. I am locking this thread to avoid confusion.

See the latest example code here:


Please note the "token" on line 49 is a Hash. From another thread it sounds the demo application works for you. You can add "pp token" to the code to inspect the hash contents and see the required values.


-Danial, AdWords API Team.
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages