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

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.

/uploads/2020/02/image-1.png?w=1024

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

/uploads/2020/02/image-3.png?w=388

/uploads/2020/02/image-2.png?w=632

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.

/uploads/2020/02/image-4.png?w=1024

/uploads/2020/02/image-6.png?w=1024

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.

/uploads/2020/02/image-7.png?w=1024

/uploads/2020/02/image-8.png?w=879

  • Go to portal.azure.com and search for functions. Select “Function App” and click on “Add”.

/uploads/2020/02/image-9.png?w=739

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

/uploads/2020/02/image-10.png?w=566

  • Once deployed, open the function app and create a new function.

/uploads/2020/02/image-11.png?w=265

  • Choose In-portal > Webhook + API > Create.

/uploads/2020/02/image-12.png?w=837

/uploads/2020/02/image-14.png?w=858

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

/uploads/2020/02/image-15.png?w=875

/uploads/2020/02/image-16.png?w=876

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

/uploads/2020/02/image-17.png?w=335

  • Testing time! You have now successfully created the function and it’s time to make sure it’s working fine. Click on Test.

/uploads/2020/02/image-18.png?w=1024

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.

/uploads/2020/02/image-19.png?w=518

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

/uploads/2020/02/image-20.png?w=632

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

/uploads/2020/02/image-22.png?w=616

/uploads/2020/02/image-23.png?w=609

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

/uploads/2020/02/2020-02-16_17-16-33.png?w=390

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 -

/uploads/2020/02/image-25.png?w=512

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

/uploads/2020/02/image-26.png?w=189

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.

/uploads/2020/02/oauth-1.0.png?w=800


jsidarta -

Thanks for the article, can we somehow embed this authentication method into a power automate custom connector?


#### [elliottliu](https://www.elliottliu.com "[email protected]") -

Thanks for sharing this! I stumbled across your article while trying to integrate with another API that used OAuth 1.0. I’ve adapted the code you provided to create a generic authorization header (https://github.com/Elliott-Liu/azure-functions-oauth-1.0).


#### [vivekbavishi](http://thatapiguy.tech "[email protected]") -

Wow. That’s awesome. I am going to try that out. Also, I saw your website and it’s impressive how you just stumbled upon this article and created something like this without much background in the subject. Kudos!


#### [krapo](http://krapo.wordpress.com "[email protected]") -

Hi! Thanks a lot for this article. I tried to follow it as best as I could but I have to say I have no idea what I’m doing. I solved several problems I encountered but this one doesn’t get me a lot of Google results so I thought I would ask for your help. I get the following error message: 2022-08-03T16:14:35Z [Error] ERROR: Response status code does not indicate success: 400 (Bad Request). Exception : Type : Microsoft.PowerShell.Commands.WriteErrorException Message : Response status code does not indicate success: 400 (Bad Request). HResult : -2146233087 CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TwitterRestMethod InvocationInfo : MyCommand : Invoke-TwitterRestMethod ScriptLineNumber : 390 OffsetInLine : 10 HistoryId : 1 ScriptName : C:\home\site\wwwroot\HttpTrigger2\run.ps1 Line : $Tweet = Invoke-TwitterRestMethod -ResourceURL $ResourceURL -RestVerb $Method -Parameters $Parameters -OAuthSettings $OAuth -PostBody $PostBody PositionMessage : At C:\home\site\wwwroot\HttpTrigger2\run.ps1:390 char:10 + $Tweet = Invoke-TwitterRestMethod -ResourceURL $ResourceURL -RestVerb … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PSScriptRoot : C:\home\site\wwwroot\HttpTrigger2 PSCommandPath : C:\home\site\wwwroot\HttpTrigger2\run.ps1 InvocationName : Invoke-TwitterRestMethod CommandOrigin : Internal ScriptStackTrace : at Invoke-TwitterRestMethod, C:\home\site\wwwroot\HttpTrigger2\run.ps1: line 307 at , C:\home\site\wwwroot\HttpTrigger2\run.ps1: line 390 PipelineIterationInfo : Thanks a lot for your help!


Did this article help you❓

Subscribe to get more Power Platform articles directly in your inbox