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?