This is a Lambda function that can be triggered upon an expired access token. Tokens here are stored in AWS Secrets Manager but can be adapted to any other storage method such as writing a JSON file to AWS S3.
This example is used with the Constant Contact v3 API. Their server-flow still requires a client to approve and receive the first set of tokens. There is an additional Lambda API in this repository to facilitate that. Once you have the first access token / refresh token your Lambda function should be self-sufficient for automation. I decided to open this repo to the public when I was struggling to find an existing example using this flow.
sam local start-api --profile YOUR_LOCAL_AWS_CLI_PROFILE
sam local invoke getUserPrivileges --event events/event-get-user-privileges.json --profile YOUR_LOCAL_AWS_CLI_PROFILE
- AWS CLI
- AWS SAM
- Constant Contact Account and Registered App (Or some other OAuth 2 API).
- AWS Secrets Manager Secret
npm install
sam local start-api --profile YOUR_LOCAL_AWS_CLI_PROFILE
- Navigate to http://localhost:3000
- Allow your application access.
- Test your stored token on an authorized API Endpoint, this example checks your user privileges.
sam local invoke getUserPrivileges --event events/event-get-user-privileges.json --profile YOUR_LOCAL_AWS_CLI_PROFILE
AWS Secrets Manager is being used to store and exchange tokens. Login to your AWS Console and navigate here to create a new secret.
- Select "Other type of secrets"
- Add two rows, one for "access_token" and one for "refresh_token". Enter any value for now.
- Name your secret and then add that name to the environment variables under
AWS_SECRET_ID
.
- "Next" > "Next" and "Store"
To run this Lambda locally, you will need to install and configure the AWS CLI. A profile and user is required to interact with AWS. More info here
Be sure to create a separate user for your machine to generate an Access Key ID.
To run this Lambda locally, you will need to install and configure AWS Serverless Application Model (SAM). You will also need Docker installed.
This example is used to connect to the Constant Contact V3 API. It can be applied to any other API using OAuth2. You can create a free account to get started.
- Navigate to My Applications and create a "New Application".
- Change the Redirect URI to include "http://localhost:3000/callback".
- Copy the API Key to the environment variables under
CC_API_KEY
.
- "Generate Secret" and copy this secret to the environment variables under
CC_API_SECRET
.
- Add "contact_data" to the environment variables under
CC_API_SCOPE
.
- Click "Save" in the Constant Contact New Application page before moving on.
- Follow the steps under "Setup" at the top of this Readme. You should be able to authorize your application to receive your first set of tokens.