Reputation: 276
My question is: How to get the token Adwords without needing to interact with the browser, but only via code
Now I run this code:
token = adwords.authorize () do | auth_url |
puts "Hit Auth error, please navigate to URL: \ n \ t% s"% auth_url
print 'log in and type the verification code:'
VERIFICATION_CODE = gets.chomp
VERIFICATION_CODE
end
And lap the token that is returned in browser url to liberate access.
Is to do all this using only Ruby code?
Thank you.
Upvotes: 0
Views: 1078
Reputation: 425
In order to get offline access to a user's AdWords data,
To ask for offline permissions, simply add the following query parameter to your OAuth2 URL:
&access_type=offline
Here is an example of a full URL requesting offline permission to the user's AdWords data:
https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=auto&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_CALLBACK_URL&response_type=code&scope=https%3A%2F%2Fadwords.google.com%2Fapi%2Fadwords%2F
Your client ID will be something like this: 111122223333.apps.googleusercontent.com
Make sure to properly escape your callback URL before substituting it in.
This way, when you get an access_token
for the account, you will also get a refresh_token
from Google in the same response. Store this in your database with the rest of the account's data (including the access_token
).
Later, when your original access token has expired, you can request a new one by making the following request:
https://accounts.google.com/o/oauth2/token?refresh_token=THE_REFRESH_TOKEN&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=refresh_token
Upvotes: 2