Purpose
To provide API to connect and use services secured by the oAuth 2.0 protocol using Lua.Dependencies
- neturl
- luajson
- luasec (default) or Lua-cURLv3
Basic Usage
The demo.lua file gets access to google drive and then prints out the response to the request to get the root directory listing in the drive.To make the example code work below follow these steps to get the credentials file from Google API for your script:
- Log in to the google developer console.
- Go to the Drive API link under the Google Apps APIs
- Enable this API and download a credentials file
- From the credentials file take the installed json table and paste in a new file creds.json and place the file in the same directory as this demo file
- Now execute the code
Example code from demo.lua in the test subdirectory:
API Reference
Module
API
The oauth2 module exposes just 1 function:new
Syntax:To create a new oAuth 2.0 object.
Usage:
Inputs:
config table to configure the new oAuth 2.0 object. The configuration table can have the following keys:
- creds_file: [OPTIONAL] Full path and name for the credentials file. This parameter if not given then the credentials must be provided in the creds table described below.
- creds: [OPTIONAL] Table containing the connection credentials. This table must have the following keys:
- client_id: String key for the client id.
- client_secret: String key for the client secret.
- redirect_uris: [OPTIONAL] Redirect URIs provided by the service
- auth_uri or auth_url: [OPTIONAL] URL string for requesting authorization. NOTE: If this is given it will overwrite the auth_uri or auth_url given in the main config table
- token_uri or token_url: [OPTIONAL] URL string for requesting token. NOTE: If this is given it will overwrite the token_uri or token_url given in the main config table
- tokens_file: [OPTIONAL] Full path and name for the tokens file. This file will be used to read the token and save the updated token. NOTE: If both the tokens_file and tokens table have valid tokens then the tokens_file token is used.
- tokens: [OPTIONAL] Table containing the access tokens for the oAuth object. This table must have the following keys:
- access_token
- token_type
- refresh_token
- expires
- scope: Scope of the access
- access_type: [OPTIONAL] The access_type parameter to be inserted in the query parameter of the endpoint
- redirect_uri: [OPTIONAL] The redirect_uri parameter to be inserted in the query parameter of the endpoint
- approval_prompt: [OPTIONAL] The approval_prompt parameter to be inserted in the query parameter of the endpoint
Returns:
- oAuth2.0 access object on success
- nil and an error message on failure
oAuth 2.0 object
API
acquireToken
Syntax:To acquire a access token for a oAuth 2.0 object
Usage:
Inputs:
- oa: The oAuth 2.0 object
Returns:
- nil and an error message on failure
- table containing URL string and token add function - The URL string is the URL to follow to obtain the token code from the online service. The second return is a function that needs to be called once the code is obtained by passing the code to the function to finish acquiring the token
request
Syntax:To carry out a https request through the oAuth 2.0 protocol
Usage:
Inputs:
- oa: The oAuth 2.0 object
- url: The url where to make the request
- payload: [OPTIONAL] Data to post to the URL. This can be a string or a table. The function implementation of this is not implemented yet so that we can have a source function to stream data.
- headers: [OPTIONAL] Table of headers to send the URL
- verb: [OPTIONAL] The https request verb. Default is GET. If there is a payload then default is POST.
Returns:
- nil and an error message on failure
- The response as a string, the https response code and a boolean flag which is true if the token was refreshed on success request completion