web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Using C# vs2022, calli...
Power Automate
Unanswered

Using C# vs2022, calling Power Automate When HTTP Request is Received problem

(0) ShareShare
ReportReport
Posted on by 4

Straight forward issue, I have written a C# test app that is supposed to post to a power automate flow, I have the URL it gives and following as many tutorials as possible, I have tried to call the URL only to always get unauthorized in return, but when I tried to pass in the credentials it then tells me:
{"error":{"code":"SecurityTokenInvalidSignature","message":"The provided authentication token is not valid, token signature is not properly formatted."}}

But the code I am using is as follows:

 

using Microsoft.Identity.Client;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
using System.Net.Http.Headers;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Configuration;

namespace LaunchPowerAutomate
{
 public partial class frmTest : Form
 {
 public frmTest()
 {
 InitializeComponent();
 }

 async Task InitCredentials()
 {
 string clientId = "CLIENTID";
 string clientSecret = "CLIENTSECRET";
 string tenantId = "TENANTID";
 string[] scopes = new string[] { "https://graph.microsoft.com/.default" }; 

 var pcaOptions = new PublicClientApplicationOptions
 {
 ClientId = clientId,
 TenantId = tenantId
 };
 var pca = PublicClientApplicationBuilder
 .CreateWithApplicationOptions(pcaOptions).Build();


 var authResult = await pca.AcquireTokenInteractive(scopes).ExecuteAsync();


 //var cca = ConfidentialClientApplicationBuilder.Create(clientId)
 // .WithClientSecret(clientSecret)
 // .WithTenantId(tenantId)
 // .Build();


 var result = await cca.AcquireTokenForClient(scopes).ExecuteAsync();

 string accessToken = authResult.AccessToken;

 // Use the access token in HTTP requests
 await MakeApiCall(accessToken);
 }
 async Task MakeApiCall(string accessToken)
 {
 using (HttpClient client = new HttpClient())
 {
 client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

 // Replace with your Power Automate API URL
 string apiUrl = "https://prod-42.westus.logic.azure.com:443/workflows/XXXXXXXXXXXXXXXXXXXXXXXXXXX";
 var response = await client.GetAsync(apiUrl);
 var content = await response.Content.ReadAsStringAsync();
 MessageBox.Show(content);
 }
 }

 private async void btnTest_Click_1(object sender, EventArgs e)
 {
 await InitCredentials();
 }
 }
}

 

This problem is pretty consistent no matter how I try to build up the access token, what am I missing? This calls the OAuth, i select the login that is an employee, the app is registered in AD app registration, etc, any help would be greatly appreciated.

Eventually I want to call a flow from C# and pass it JSON that includes 2 encoded files as upload into the flow to use the AI model to review in an unstructured document, I have that flow working individually, but not with an HTTP post, I have to trigger it manually and select the PDF's for it to work, I want to have it work via a C# windows forms app and also a web application on the internal portal, but in both cases I have to call the flow via that API call and in all cases it is telling me that the token signature is not properly formatted, how do I format it?

Categories:
I have the same question (0)
  • rbasten-DNB Profile Picture
    2 on at

    Hi, 
    Did you ever get this to work? 
    I'm facing the same requirement because we user specflow to trigger some flows and we also want to switch from "Anyone" to "Users in the tenant".
    I have included a token bearer but it's giving the error 401.

  • jbwgames Profile Picture
    4 on at

    Does anyone know of a solution to this problem? I am trying to make the call via a tenant account, not a personal account, as such, you have to pass in authentication, I prompt for the user account, I am able to get the access token, but when I send the token, I still get errors, is there some escalation point I can reach out to at Microsoft?

  • jbwgames Profile Picture
    4 on at

    Thanks, but he doesn't use security and his code doesn't work in the current workflow for the HTTP Request is Received, his videos is pre-OAUTH security model and doesn't work for workflows written and sitting within an OU, likely he created that video on a personal account and not a corporate account.  His model does not work at all because it has no authentication.  He is calling an HTTP post event passing json, then reading the json to send the email, but the problem is, his call to his URL is unauthenticated and has no OAuth 2.0 in it.  So with corporate flows, you have to tell the security on the flow one of 3 things, "Any User in my Tenant", "Specific User in my Tenant", and "Anyone", of those three there are 3 different security models (none of which his video covers)

    First, the "Any User in my Tenant" requires:

     

    {
     "error": {
     "code": "DirectApiAuthorizationRequired",
     "message": "The OAuth authorization scheme is required. Please add authentication scheme and try again."
     }
    }

     

    Next, the "Specific user in my Tenant", where you specify the exact allowed users in a list of allowed users, but still, same schema error as above without the OAuth written in the code, with the same flaw as the initial post I mentioned, then finally the "Anyone" which is a different model and a different problem because I do not want un-authenticated users posting to the workflow URL.

    Many thanks again but I have watched that video and it doesn't work for Tenant level flows with Authentication.

    Trying to solve the authentication OAuth 2.0 problem.

    Even if I switch to "Any" for authorization, to try to follow his example, that gives me other problems such as:

    {"error":{"code":"OAuthAccessPolicyNotFound","message":"Authorization' header is not allowed. The OAuth authentication policy is not enabled for the workflow."}}

    Removing the Authorization header then yields:

    {"error":{"code":"DirectApiAuthorizationRequired","message":"The request must be authenticated only by Sared Access scheme."}}

    I have yet to find any solution to all 3 problems, all boiling down to OAuth issues and no documentation with working examples.

  • Pstork1 Profile Picture
    68,863 Most Valuable Professional on at

    Have you tried this walkthrough example?  How to Run Power Automate Flow using API in C# - JD Bots (jd-bots.com) That may give you what you need.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 262 Super User 2025 Season 2

#2
Expiscornovus Profile Picture

Expiscornovus 211 Most Valuable Professional

#3
trice602 Profile Picture

trice602 198 Super User 2025 Season 2

Last 30 days Overall leaderboard