OAuth 1.0 Connection in Power Automate | Post Tweets with user mentions and send DMs using Twitter API

In this post, you will learn how to connect to an API with OAuth 1.0 authentication in Power Automate. Using this connection, we will connect to Twitter API to post tweets with @mentions and also send DMs with quick actions. The Twitter connector available in Power Automate doesn’t allow user mentions and also doesn’t have an action to send DMs.

Currently, it’s not possible to implement OAuth 1.0 directly in Power Automate as the HTTP action doesn’t have this type available in the list of authentication types. To overcome that, we will create an Azure function which connects to the Twitter API and then call the Azure function from Power Automate. Don’t worry if you haven’t created an Azure function before. I will walk through all the steps to create an Azure function as well

Credits

Before we start, credits to Michael LaMontagne for his blog post on a similar topic. I could use it fine for posting tweets , however, for sending DMs it got a bit tricky as I had to include some body in the POST requests. Also, credits to Geetha Sivasailam for helping me with some of the PowerShell stuff. I didn’t know anything about PowerShell before this.

Let’s start building!

Step 1 – Create a Twitter developer account to use the Twitter API

Create an app
  • Enter the required details – App Name, description, website URL and details on how the app will be used. Agree to the developer terms and create the app.
Connection in Power Automate
App Details
Review terms

Awesome! you have now created a Twitter App.

  • Go the Permissions section , click on Edit and choose the “Read, write and Direct Messages” option and hit save.
Connection in Power Automate
Permissions
Read, write and direct messages

4. Go to the “Keys and Tokens” section and click on generate to get the access token and access token secret. Copy them and save it securely. Also copy the API Key and API secret key.

Connection in Power Automate
Keys and tokens
Please save your tokens

Step 2 – Create the Azure Function to connect to Twitter API

  • Go to portal.azure.com and search for functions. Select “Function App” and click on “Add”.
Connection in Power Automate
Azure functions
  • Select the subscription (if you don’t have one, setup a “Pay as you go” subscription), select the resource group or create a new one , define a function app name and choose the runtime stack and region. Then click on ‘Review+create’. Once the data is validated, select create and wait for some time for the function app to be deployed.
Function App
  • Once deployed, open the function app and create a new function.
Connection in Power Automate
Function Apps
  • Choose In-portal > Webhook + API > Create.
webook -API
  • Then go to the Application Settings of the function app and add the Twitter API keys and tokens there. I used these names – TwitterApiKey, TwitterApiSecret, TwitterAccessToken, TwitterAccessTokenSecret which I have referenced in the run.ps1 file (covered below)
Configuration
Configuration
  • Now, copy the raw PowerShell code from here – run.ps1 (which I modified a bit from the version that I got from this blog post mentioned above in the credits. ) and paste it to your function’s run.ps1 file.
Connection in Power Automate
HttpTrigger
  • Testing time! You have now successfully created the function and it’s time to make sure it’s working fine. Click on Test.

Paste this in the Request body –

123456{"Resource": "statuses/home_timeline.json","Method": "GET","Parameters":"","PostBody":""}

This should return an Output with your home timeline of tweets. If it doesn’t , check the steps mentioned above once more and try again to test. If it still doesn’t work, write a comment below with the issue that you are facing.

If it works, YAYYY!!! You have done an awesome job! Now you are ready to use the Twitter API to basically do anything that the API allows.

Before we create the flow , copy the function URL and save it somewhere. You will need it in the HTTP action.

run.ps1

Step 3- Create the flow in Power Automate

Add the “Manually trigger a flow” trigger and HTTP request as an action.

HTTP
  • To create a tweet with a user mention, use the below in the body
{
"Resource": "statuses/update.json",
"Method": "POST",
"Parameters":{
"status":"This tweet was posted using an Azure function and @MSPowerAutomate"
},
"PostBody":""
}
POST
Connection in Power Automate
Tweet
  • To send a DM to a user , you first need the user id (not the screen name/handle). The user id is a number which you can find either by searching for the user with API or just send a DM to the user and you will find it in the URL. One of numbers is your own user id and other one is the person who you are sending the message to. Also, to send DMs the person should follow you otherwise you can’t send.
User ID

Once you have the user id, use the following in the body of the HTTP action.

{
  "Resource": "direct_messages/events/new.json",
  "Method": "POST",
  "Parameters":"",
  "PostBody":'{
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": "user-id"
      },
      "message_data": {
        "text": "This is a test DM"
	}
     }
   }
 }'
}

Power Automate somehow finds this as invalid JSON and thus I followed this approach –

Connection in Power Automate
Manually trigger a flow
  • You can also send options in a DM using this –
{
  "Resource": "direct_messages/events/new.json",
  "Method": "POST",
  "Parameters":"",
  "PostBody":'{
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": "user-id"
      },
      "message_data": {
        "text": "A DM with option ",
        "ctas": [
          {
            "type": "web_url",
            "label": "Open Google.com",
            "url": "https://google.com"
          },
          {
            "type": "web_url",
            "label": "Open Microsoft.com",
            "url": "https://microsoft.com"
          }
        ]
		}
    }
  }
}'
 }
Connection in Power Automate
A DM with option

You can basically use this to access anything under the Twitter API hood. For more info visit – https://developer.twitter.com/en/docs/api-reference-index

Also, you can follow a similar approach for any other API that uses Oauth 1.0 as the authentication method.

About the Author:

Vivek Bavishi, also known as the API Guy, is a Power Platform Microsoft MVP.

Reference:

Bavishi, V. (2020). OAuth 1.0 Connection in Power Automate | Post Tweets with user mentions and send DMs using Twitter API. Available at: https://thatapiguy.tech/2020/02/16/oauth-1-0-connection-in-power-automate-post-tweets-with-user-mentions-and-send-dms-using-twitter-api/ [Accessed: 1st April 2020].

Find more great Power Platform content here.

Share this on...

Rate this Post:

Share: