This guide will walk you through how to setup OAuth2 for API access using your own credentials using installed application flow. These steps only need to be done once, unless you revoke, delete, or need to change the allowed scopes for your OAuth2 credentials.
Step 1 - Creating OAuth2 credentials
Generate a client ID and secret by following the linked instructions, then come back to this page.
Step 2 - Setting up the client library
google-ads.yamlfile, insert your client ID and secret.
client_id: INSERT_OAUTH2_CLIENT_ID_HERE client_secret: INSERT_OAUTH2_CLIENT_SECRET_HERE
Go back to the Credentials page where you retrieved your client ID and client secret and click the button on the right-hand side of the screen to download your credentials as a JSON file. Save that file on your machine in a location that's easy to remember, such as in your $HOME directory.
If you haven't already, clone the client library from GitHub and install its dependencies by running the below commands from a terminal. It's recommended that you conduct these steps from within a virtual environment. For more information see the Virtual Environment documentation.
$ git clone firstname.lastname@example.org:googleads/google-ads-python.git $ cd google-ads-python $ pip install .
Once you've installed the library and its dependencies you can optionally run the tests just to make sure everything is set up properly:
$ python setup.py test
Next, navigate to the OAuth2 example, authenticate_in_standalone_application.py.
$ cd examples/authentication
Run this example via the command line, passing in the absolute path to the JSON file generated earlier.
$ ./authenticate_in_standalone_application.py --client_secrets_path=/path/to/secrets.json
The example will prompt you to visit a URL where you will need to allow the OAuth2 credentials to access your Google Ads account on your behalf.
- Navigate to the URL in a private browser session or an incognito window.
- Log in with the same Google account you use to access AdWords.
- Click Allow on the OAuth2 consent screen.
An authorization code will be shown to you. Copy and paste the verification code into the command line where you're running the
authenticate_in_standalone_application.pyexample and press enter. The example should complete and display an offline refresh token.
After approving the token enter the authorization code here: **** Your refresh token is: ****
Copy the generated refresh token into your
google-ads.yamlor save it somewhere else to use it to instantiate the library at runtime.