It is convenient to be able to interact with a Google Sheet remotely using a script running on a local computer. This can automate updates, analyses and many other tasks involving a spreadsheet. Furthermore, this approach is also likely to apply to other Google Drive components like Doc.
Lines 1 to 3 are including the necessary modules. So make sure the necessary modules are installed. NPM does find and install most packages quite readily in case you forget to install some of the modules.
Line 6 specifies a “scope”. Refer to Google scopes for a longer list and explanation. This scope can be changed to without .readonly to permit changes to the spreadsheet.
TOKEN_PATH is a constant so that various places of the program can refer to the “token” at the same path.
13 attempts to read a credential file. A credential file should exist as credential.json in the same folder as the script prior to running the script. This file essentially enables the script to sign in as whatever Google identity was used to create the API project, and under the name of the project.
On line 16, the subroutine authorize is called. It has two parameters. The first one is the credential object that is parsed from the file credential.json, the second one is a callback function in case authorization is successful.
While it is unlikely that you need to modify the function authorize, it is nonetheless helpful to understand it. It attenpts to read a “token” file. If such a file exists, the token object represented by the file is used to authenticate using OAuth 2.0. Otherwise, a new token is acquired using the client ID and secret contained in the credential object.
In OAuth 2, when a server authorizes based on a specific credential, the server returns with an authorization code otherwise known as a “token.” If the script does not yet have the authorization code, then the subroutine getNewToken is called to get authorized. Otherwise, an existing token is read so that the client can reuse the same authorization code again. The callback subroutine is called with a single parameter that specifies the authorization code.
Line 44 starts the subroutine to get a new token (aka “authornization code”). Again, it is unlikely that your script will need to modify this subroutine, but it is helpful to understand how it works.
line 45 interacts with an OAuth server to generate a unique URL. This URL must be visited using a browser. When this URL is visited, it allows the user to specify a Google account, and understand what kind of access is about to be granted to the script. Upon successful sign in or specification of a Google account and agreement to the access of the resource, a code is presented.
This code should then be copied-and-pasted on the command line to complete the negotiation with the OAuth 2 server. The code is passed as the first parameter to oAuth2Client.getToken. If the code matches what the OAuth server expects, then a token is generated by the getToken method of the oAuth2Client object. The token is then passed to a callback function.
The callback (second parameter of oAuth2Client.getToken) of this method then performs two important operation. First, it associates the token with the credential property of the oAuth2Client object so that from here on, the token is the key of accessing the resources. Second, it stores the otken as a file for further use.
With an OAuth2Client object and credential specified, the application specific callback is finally called. This subroutine starts on line 74. This is where the Google Sheets NodeJS API can be used.
On line 75, the main Google Sheets API object is created with the authentication object. This API object then used on the next line to get specific cells of a specific sheet of a specific spreadsheet using the get method. A callback is specified as the second argument to process the error or result of the operation.
A credential file is a form of authentication without interactively supply a password.
Go to the Google Sheets API Guides page and click “enable the Google Sheets API” button to start or select a project and get the credential file. Note that this wizard also calls the credential file “client configuration”, but that is what you need to download as the credential.
OAuth 2 is a client-server authentication mechanism. A “client” is an entity that requests authentication to a resource of a server. In this case, the script is the client. A client specifies the following:
A token is a crypto object created by an OAuth 2 server after authentication and access granting are completed. As such a token is the crypto representation of successful authentication and can be used to attempt to gain access to applications. A token is attached to an OAuth 2 client object once it is either read from local file system or generated by interactively authenticating and being granted access.
The OAuth flow is as follows: