diff --git a/README.md b/README.md index e212803d0..8a4374f36 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,13 @@ After a brief delay, you will receive the text message on your phone. > **Warning** > It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out [How to Set Environment Variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information. +## OAuth Feature for Twilio APIs +We are introducing Client Credentials Flow-based OAuth 2.0 authentication. This feature is currently in beta and its implementation is subject to change. + +API examples [here](https://github.com/twilio/twilio-node/blob/main/examples/public_oauth.js) + +Organisation API examples [here](https://github.com/twilio/twilio-node/blob/main/examples/orgs_api.js) + ## Usage Check out these [code examples](examples) in JavaScript and TypeScript to get up and running quickly. diff --git a/examples/orgs_api.js b/examples/orgs_api.js new file mode 100644 index 000000000..e85b3848e --- /dev/null +++ b/examples/orgs_api.js @@ -0,0 +1,37 @@ +"use strict"; +var Twilio = require("../lib"); + +const clientId = process.env.ORGS_CLIENT_ID; +const clientSecret = process.env.ORGS_CLIENT_SECRET; +const accountSid = process.env.TWILIO_ACCOUNT_SID; +const organizationSid = process.env.TWILIO_ORG_SID; + +const orgsCredentialProvider = new Twilio.OrgsCredentialProviderBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .build(); + +const client = new Twilio(); +client.setCredentialProvider(orgsCredentialProvider); +client.setAccountSid(accountSid); + +client.previewIam + .organization(organizationSid) + .accounts.list() + .then((accounts) => { + console.log(accounts); + }) + .catch((error) => { + console.log(error); + }); + +client.previewIam + .organization(organizationSid) + .accounts(accountSid) + .fetch() + .then((account) => { + console.log(account); + }) + .catch((error) => { + console.log(error); + }); diff --git a/examples/public_oauth.js b/examples/public_oauth.js new file mode 100644 index 000000000..3f2ab8e73 --- /dev/null +++ b/examples/public_oauth.js @@ -0,0 +1,25 @@ +var Twilio = require("../lib"); + +const clientId = process.env.OAUTH_CLIENT_ID; +const clientSecret = process.env.OAUTH_CLIENT_SECRET; +const accountSid = process.env.TWILIO_ACCOUNT_SID; + +const clientCredentialProvider = new Twilio.ClientCredentialProviderBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .build(); + +const client = new Twilio(); +client.setCredentialProvider(clientCredentialProvider); +client.setAccountSid(accountSid); + +const messageId = "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; +client + .messages(messageId) + .fetch() + .then((message) => { + console.log(message); + }) + .catch((error) => { + console.log(error); + });