Getting Google OAuth Access Token using Google APIs

OAuth access token for Google Drive

Osanda Deshan Nimalarathna
Test Automation Master
3 min readSep 15, 2018

--

If you need to access your Google drive and read your contents through an API, you will need the Google OAuth access token associated with your google drive.

Here I have explained how to get that access token using Google APIs.

  1. Go to this link.
  2. Login to your Google account.
  3. Create a project using a project name and click on “CREATE” button.
Creating the project on your google account

4. Select the project you have created.

Selecting the created project

5. Click on “Open” button.

Open the created project

6. To enable Google Drive API, click on Google Drive API and then click on “ENABLE” button.

Enabling Google Drive API

7. Click on Create Credentials button.

Creating credentials

8. Choose Web browser (Javascript)” for the “Where will you be calling the API from?” dropdown.

9. Select “ User data” for the “What data will you be accessing?”.

10. Click on “What credentials do I need?” button.

Creating credentials for the project

11. Provide a redirect URL for the “Authorized redirect URIs”.
Ex:
https://www.maxsoftlk.com

12. Click on “Create OAuth client ID” button.

Creating OAuth client ID

13. Provide a product name”.
Ex: Test1

14. Click on “Continue” button.

15. Click on “Download” button to download this credential information in JSON format.

16. Click on “Done” button.

17. Change this URL according to the parameters given by you in the above steps.

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive&response_type=code&access_type=offline&redirect_uri=redirect_url&client_id=your_client_id

Note: Your Client ID can be found from the file you have downloaded in step 15.

Ex: https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive&response_type=code&access_type=offline&redirect_uri=https://www.maxsoftlk.com&client_id=1072307075112-fposi9hsdfghjkd3239f6gbfpkfasfpk.apps.googleusercontent.com

18. Copy the above URL and paste it in an incognito mode of your browser.

19. Select the Google account which you have logged in previously.

20. If you are come-up with this screen, click on Advanced link and then click on go with unsafe mode link.

Screen 1
Screen 2

21. Click on “Allow” button.

Allowing the redirect uri to access your google drive

22. Copy the URL on the address bar.

Copying the url in the address bar of the browser

23. Paste it in the note pad and copy the code.

Ex: https://www.maxsoftlk.com/?code=4/WwBf2eVqGtlxmWEGHGymGi18OJCj3JFMhXouWO1jrdnyWGqH4pcNoprdcFGwTWA_AG-sX3nRqEUn_Z2x0WGHhQg&scope=https://www.googleapis.com/auth/drive#

The code is: 4/WwBf2eVqGtlxmWEGHGymGi18OJCj3JFMhXouWO1jrdnyWGqH4pcNoprdcFGwTWA_AG-sX3nRqEUn_Z2x0WGHhQg

24. Now you can use these details to invoke the Google API and get the Google OAuth Access token for the Google Drive.

25. Open the Postman

26. Create a new request with these details.
HTTP Method: POST
Body Type: form-data

Key-Value pairs for the form-data request

27. Click on “Send” button and get the access token.

Invoking the request through postman

Note: Postman Collection

Happy Programming !!!

--

--

Osanda Deshan Nimalarathna
Test Automation Master

Founder of MaxSoft | RPA Solution Architect | Open-source Contributor | Automation Framework Developer | Technical Specialist