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

  • 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.

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.

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.

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”.
  • 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.
  • Once deployed, open the function app and create a new function.
  • Choose In-portal > Webhook + API > Create.
  • 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)
  • 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.
  • 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 –

{
"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.

Step 3- Create the flow in Power Automate

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

  • 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":""
}
  • 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.

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 –

  • 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"
          }
        ]
		}
    }
  }
}'
 }

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.

If you like this post, please subscribe to get the latest from my blog.

[jetpack_subscription_form show_only_email_and_button=”true” custom_background_button_color=”undefined” custom_text_button_color=”undefined” submit_button_text=”Subscribe” submit_button_classes=”undefined” show_subscribers_total=”false” ]

Related Posts

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

Leave a Reply

%d bloggers like this: