Skip to main content

Notifications

Power Apps - Connector Development
Unanswered

Custom connector with application/x-www-form-urlencoded works everywhere except Power Apps

(0) ShareShare
ReportReport
Posted on by 250
My custom connector is meant to connect to a locally hosted REST API endpoint. It mostly uses Bearer authentication (except, of course, in the Token request, where you log in).
 
Making a request for a Token is very simple:
 
Method POST
URL http://hostaddress/Token
Accept application/json, application/xml
Content-Type application/x-www-form-urlencoded
Body userName=NAMEHERE&password=PASSHERE&grant_type=password
 
With these, I can get a bearer token when writing my own code outside of Power Platform. Having already built a working connector for this endpoint (which used bearer tokens obtained from an automation outside Power Platform), I decided to simplify things and added this request as an Action in that connector.
 
This is how the Token action is being defined in the custom connector:
 
Header Name Default value Is required? Visibility Type Format
Accept application/json, application/xml Yes internal string  
Content-Type application/x-www-form-urlencoded Yes internal string  
 
Body item Name Default value Is required? Visibility Type Format
body N/A (this is the root) Yes none N/A N/A
key-body-output   Yes none string  
 
Here are the Policies in this connector, in order (originally, it was just the first one, but the other two were added to try to fix my issue):
 
Name Template Operations Header name Header value Action if header exists Run policy on
Set HTTP headers Set HTTP header all Actions except Token Authorization @headers('Access-Token') override Request
Add X-Content-Type-Options nosniff Set HTTP header Token X-Content-Type-Options nosniff override Request
Set Content-Type to application/x-www-form-urlencoded Set HTTP header Token Content-Type application/x-www-form-urlencoded override Request
It works when I test it on the custom connector page, and it works in Power Automate. But using it in Power Apps always causes this error:
 
{
  "error": "unsupported_grant_type",
  "debugInfo": "clientRequestId: ab506935-dfff-4069-b1fa-4f6774a1a3a0"
}
 
 
When using the Monitor, it seems like the Power Platform is possibly overwriting the Content-Type header with its own when I try using it in Power Apps, based on the data.request portion of the Monitor entry:
 
    "request": {
      "url": "https://unitedstates-002.azure-apim.net/apim/CONNECTORURL/MORECONNECTORURL/Token",
      "method": "POST",
      "headers": {
        "Accept": "application/json, application/xml",
        "x-ms-user-agent": "PowerApps/3.24121.10 (Web AuthoringTool; AppName=GUIDHERE)",
        "x-ms-client-session-id": "SESSIONID",
        "x-ms-client-request-id": "REQUESTID",
        "x-ms-client-environment-id": "/providers/Microsoft.PowerApps/environments/ENVIRONMENTID",
        "x-ms-client-app-id": "/providers/Microsoft.PowerApps/apps/CLIENTAPPID",
        "x-ms-client-tenant-id": "TENANTID",
        "x-ms-client-object-id": "OBJECTID",
        "Accept-Language": "en-US",
        "Cache-Control": "no-cache, no-store",
        "Content-Type": "application/json",
        "x-ms-request-method": "POST",
        "x-ms-request-url": "/apim/CONNECTORTHINGS/MORECONNECTORTHINGS/Token"
      },
      "body": "userName=NAMEHERE&password=PASSHERE&grant_type=password"
    }
 
Instead of having the expected Content-Type value of "application/x-www-form-urlencoded", Power Apps is sending "application/json" (or, at least, it seems to be). Based on other threads here, this seems to be a common problem.
 
I have tried the following fixes, though they haven't worked:
  1. Changing the header visibility values from "none" (their original value) to "internal" (their current value). It didn't fix the problem, but it kept Power Apps from suggesting these values to me when writing code, which was convenient. (While the value was "none", i also tried explicitly stating the header values in Power Apps, but it didn't fix it either).
  2.  Adding the "Set Content-Type to application/x-www-form-urlencoded" Policy to my connector, as seen above (commonly suggested, but did not fix the issue)
  3.  Adding the "Add X-Content-Type-Options nosniff" Policy to my connector, as seen above (a rarer suggestion, but it didn't work either, regardless of whether it placed it before or after "Set Content-Type to application/x-www-form-urlencoded" in the Policies list order.
I've had issues like this before, where something seems to work everywhere except for Power Apps. Is there a solution for this one, or will I have to resort to Flows to get tokens?
Categories:

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #9 Get Recognized…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,867

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,174

Leaderboard

Featured topics