How to implement Google OAuth2 in your script if you only have back-end (no front-end) and cannot support redirections
Create a new Project in Cloud Console:
- Navigate to https://console.cloud.google.com/ and create a new project
- Enter project name
- Click Create
- Select the newly created project from the notifications pop-up on the right
- Click on search bar at the center
- Enter spreadsheets
- Select Google Sheets API from the search results under the Marketplace
- After getting re-directed to Google Sheets API page click Enable button
- After page load click on Create Credentials on top right
- After page load click on Credential Type 'Which API are you using?' input field and select Google Sheets API
- Click on User data radio button
- Click Next
- Enter App name 'SpreadSheetApp'
- Enter your gmail id in User Support Email field
- Enter your gmail id in Developer Contact Information field
- Click Save and Continue button
- Click on Add or Remove Scopes button
- Enter spreadsheet in the search bar in the Update selected scopes popup on right
- Find 'https://www.googleapis.com/auth/spreadsheets' in the results and click on the checkbox to select it
- Click on Update button
- Click on Save and Continue button
- Select Application type 'Web Application'
- Click Add URI button under 'Authorized redirect URI's
- Enter http://localhost in the URI's input field
- Click Create
- Copy Client-ID and download the credentials file
- Click on 'OAuth consent screen page' below Download button
- Click on Add Users button under Test Users
- Enter your own email id in the Add Users popup on the right
- Click Save
- Click on Done button
- Open credentials file downloaded in Notepad++ and extract client_secret value
- Download the collection share in the github project
- Import the collection in Postman
- A collection with name 'Spreadsheet Authentication' will be shown
- Select the first API GEt request from the collection
- Click on Params
- Replace scope with https://www.googleapis.com/auth/spreadsheets
- Set access_type value offline
- Set redirect_uri value http://localhost
- Set response_type value code
- Set client_id value copied from cloud console
- Now copy the generate url from the top input field and paste in the browser
- Hit enter
- Copy the redirect url in Notepad++
- Extract code
- Click on the second Post request in the collection
- Replace code value with the newly extracted code
- Replace client_id with downloaded client_id
- Replace client_sected with extracted client_secret from credentials.json file
- Replace redirect_uri with http://localhost
- Click Send button on the api call
- You'll receive access_token, refresh_token and some other values in response
- Access Token is the value used to perform API calls
- In case access_token is expired then refresh_token can be used to generate new access_token
- For generating new access_token copy the refresh_token from the response of 2nd API Post request in the collection
- Select the 3rd API Post request in the collection and replace with refresh_token value
- Replace client_id and client_secret with the downloaded and extracted values
- Click on Send button