Authenticate Standard Logic Apps using OAuth 2.0 Bearer Token
Published Mar 30 2022 11:39 AM 6,904 Views

Sometimes we may need our apps to authenticate autonomously without user input. For this we have OAuth 2.0 Bearer Token. We use OAuth 2.0 Bearer Token to authenticate requests on behalf of our apps. For this example, we will authenticate to the Twitter API using a bearer token generated by passing our API key and Secret through the Twitter oauth2/token endpoint (OAuth 2.0 | Docs | Twitter Developer Platform).

 

What you will need for this tutorial:
1. Basic Azure Subscription for your logic app (Create Your Azure Free Account Today | Microsoft Azure).
2. Basic Twitter Developer account to access the API (Twitter Developers).
3. Postman (Download Postman | Get Started for Free)

 

Let’s start by setting up your Logic App. Once you have your Azure account, From Home, select “Create a resource” and type in logic app in the search bar then select “Create”.

 

TheComputerTutor_0-1648505554466.png

 

 

TheComputerTutor_1-1648505554472.png

 

 

  1. Select your Subscription and Resource Group. You may select Create new if you haven’t created a Resource Group yet.
  2. Give it a unique logic app name
  3. Select Workflow
  4. Select the region you would like the logic app in. I selected EastUS
  5. Now select the “Standard” option then “Review + create” then “Create”.

TheComputerTutor_2-1648505554477.png

 

 

Once your logic app is deployed select “Go to resource”. Here from the “Overview” blade you can see a few details of your new logic app. Copy the URL of the logic app from the top right corner of the overview and paste in a text file for now as we will need this later when setting up our Twitter developer account.

Next, we will set up your Twitter developer account. There are several options for the Twitter developer account. Applying for the “Hobbyist” and “Exploring the API” account should be sufficient.

TheComputerTutor_3-1648505554483.png

apply for a Twitter developer account

 

Once your developer account is set up you will need to register an app using the “Create an App” button in the top right. Complete the form with the required details, entering the Website URL from the URL of your logic app you just copied from the overview blade. Then select the “Details” button of the app once complete.

TheComputerTutor_4-1648505554484.png

create an app

 

From the apps details page, select the “Keys and tokens” tab and if not already there, click the “Generate” button to generate an API key and API secret key. Copy these and paste in the same text file as we will use this in our logic app to generate the bearer token.

TheComputerTutor_5-1648505554486.png

Twitter API keys

Now let’s go back to the logic app and from the “Workflows” blade of the logic app select “Add” to add a new workflow. Give the new workflow a name, select “Stateful” and then select “Create”.

TheComputerTutor_6-1648505554490.png

add a new workflow

 

Once created, click the name to open the new workflow. From there select the “Designer” blade to add functionality to the logic app using the designer. First, our logic app needs a trigger. For now, we will go with an HTTP request trigger. We will make a call to this trigger via Postman and in the request, pass along a search term for use later in the workflow. Select “Choose an operation” and in the panel to the right type ‘‘request’’ in the search. Select the “When an HTTP request is received” trigger. To accept a search term with this trigger, in the “Request body JSON schema” add an object with a single parameter of “SearchString” like:

{“SearchString”:“testSearchString”}

Then click Save.

TheComputerTutor_7-1648505554494.png

 

select request trigger

TheComputerTutor_8-1648505554495.png

 

After saving, go back to the Overview blade and notice your “Workflow URL” under Essentials. This is the URL we will make a POST call to from Postman to trigger our logic app.

Note that although a Twitter trigger exists for logic apps, due to certain inherent limitations in the connector and restrictions on the Twitter API (Twitter — Connectors | Microsoft Docs) this connector was not used for this demonstration.

Next, we will have to parse the content coming from the trigger to be able to use later in the workflow. Let’s select the “+” button to add another step in the workflow. Type ‘parse’ in the search box and select the “ParseJSON” data operation.

TheComputerTutor_9-1648505554498.png

 

Click in the “Content” field to select the dynamic content “Body” which is the body of the HTTP response we get back from the trigger. For the “Schema” field, click “Use sample payload to generate schema” link and in the pop-up dialog you can enter the same object we used with the trigger:

{“SearchString”:“testSearchString”}

TheComputerTutor_10-1648505554501.png

configure ParseJSON

 

Now select the “+” button to add another action to execute. Here we will type in ‘variable’ in the search and select the “Initialize variable” action. Enter a relevant name of your choice for the variable. I am using “bearerToken” for a name, set the type to “string” and left the value blank.

TheComputerTutor_11-1648505554505.png

 

Next select the “+” button again to add another action to execute. This time we will type ‘http’ in the search to use an http connector to make the request that retrieves the bearer token.

The HTTP connector parameters go as follows:

  1. Method = POST
  2. URI = the Twitter API token endpoint: “https://api.twitter.com/oauth2/token” See (POST oauth2/token | Docs | Twitter Developer Platform)
  3. Headers = a key named “Authorization” with a value of “Basic yourAPIkey:yourAPIsecretkey
  4. Queries = “grant_type” with a value of “client_credentials”
  5. Select “Add new parameter” and check the “Authentication” check box then click anywhere in the HTTP configuration box. From the Authentication type dropdown select Basic as the Authentication type. For the Username use yourAPIkey and for the Password use yourAPIsecretKey from your Twitter developer account.

TheComputerTutor_12-1648505554508.png

HTTP connector configuration

 

Now, let’s click save at this point. So far we have what we need to at least make the request to get a token, but in order to have something useful we will need to parse the object coming back from Twitter using a ParseJSON action, only there is a trick to it.

From here let's go to the overview blade of our logic app. Let’s select “Run Trigger” to get a fresh run, then select the run by clicking on the Identifier number under the run history.

TheComputerTutor_13-1648505554512.png

run history

 

If this run is successful, you should see three green checkmarks. Click to highlight the HTTP action and notice the details blade on the right. Scroll down a little to the Outputs section and notice the body which includes your bearer token. You will copy this and paste it in your text file again as we will use this to set up our next step.

TheComputerTutor_14-1648505554515.png

copy the body of the HTTP action output

 

Now, click the “X” in the top right corner to get back to the overview blade of the logic app (I like to keep two separate tabs open and just go back and forth between design and run tabs). Navigate back to the “Designer” and select the “+” sign again to add the next step in the execution. Now we will enter ‘parse’ in the search and select the “ParseJSON” data operations action. We will use this to parse the object coming back from Twitter which contains the bearer token we need. Click in the “Content” box to highlight and be able to select the dynamic content “Body” that is coming from the response of the HTTP connector action.

TheComputerTutor_15-1648505554520.png

add body of HTTP response to ParseJSON action

 

For the “Schema” you will select the “Use sample payload…” link at the bottom and paste the “body” you pasted to your text file from the last run into this dialog box and click Done.

TheComputerTutor_16-1648505554524.png

 

enter body from HTTP response in previous run as schema

 

Now click the “+” again type ‘variable’ in the search and select the “Set variable” action. Here we will set the bearerToken variable. In the “Name” drop down select “bearerToken” variable name we initialized earlier. In the “Value” field use the dynamic content of “access_token” coming back from the previous ParseJSON step. Click Save.

TheComputerTutor_17-1648505554527.png

set the bearerToken variable

 

We now have everything we need to authenticate to Twitter with our bearer token. To test, let’s add another HTTP action to get some Twitter data.

Click the “+” again to add another step. Type ‘http’ in the search and select the HTTP action. In the configuration pane add the following:

  1. Method = GET
  2. URI = the Twitter API standard query: “https://api.twitter.com/1.1/search/tweets.json?q=SearchString&result_type=recent” See (Building standard search queries | Docs | Twitter Developer Platform) using the dynamic content from the parsed trigger as “SearchString”.
  3. Headers = a key named “Authorization” with a value of “Bearer ” then add the dynamic content of the “bearerToken” variable we set earlier making sure to leave a space between ‘Bearer’ and the token.

TheComputerTutor_18-1648505554535.png

 

Give your logic app a test run by using Postman to send a request to the workflow URL on the Overview blade of your logic app sending the search string in the body of the request. You should expect to see a 200 status from Postman.

TheComputerTutor_19-1648505554544.png

 

Postman test

Now go back to the Overview blade of your logic app. You should expect to see a new run with Succeeded status. Click on the Identifier of the run and highlight the last HTTP request. On the details pane to the right scroll down to Outputs and in the Body is your collection of Tweets matching the search string you tested with.

TheComputerTutor_20-1648505554550.png

 

 

 

Co-Authors
Version history
Last update:
‎Mar 28 2022 03:21 PM
Updated by: