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
- Go to https://developer.twitter.com/en/apps and create a New App. If you don’t have a developer account, you can just create one for free.
- 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.
Comments:
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!