In order to get offline access to a user's AdWords data,
- They must grant your app offline permissions
- You must store and use a refresh token in order to get new access tokens after the initial one expires.
Asking for Offline Permissions
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.
Using a Refresh Token
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