Skip to main content

Notifications

Power Automate - Using Connectors
Answered

Generic Oauth 2 connector not refreshing access or refresh tokens

(1) ShareShare
ReportReport
Posted on by 21

Hi all,

 

I am building a custom connector to make a couple of calls into the MYOB AccountRight API (https://developer.myob.com/api/accountright/api-overview/authentication/) and everything works, except that Power Automate doesn't seem to be refreshing the access/refresh token (and it did last time I looked at this a couple of months ago). The access token expires after 20 minutes and the refresh token after 7 days. I manually fix the connection, but within 20 minutes, it no longer connects and requires fixing again. 

 

I have noted as well that the custom connector security screen shows all the values except the Refresh URL, which is empty but mandatory. Even if I update the connection details to include the refresh URL, it doesn't have any impact and the value is cleared after updating the connector.

 

The connection details are:

ID Provider: Generic Oauth 2

Client ID: xnxx6js6mv2krartysp9ddkt

Authorization URL: https://secure.myob.com/oauth2/account/authorize

Token URL: https://secure.myob.com/oauth2/v1/authorize 

Refresh URL should be: https://secure.myob.com/oauth2/v1/authorize

Scope: CompanyFile

Redirect URL: https://global.consent.azure-apim.net/redirect

 

When the custom connector isn't connected, it shows the error below. I am not sure if it is an issue or being returned by the API or somewhere in Power Automate, or if it is a symptom of another issue.

 

 

Failed to refresh access token for service: oauth2. Correlation Id=0fd144f6-e2bd-4b3a-8a7d-c2748a8767c2, UTC TimeStamp=11/4/2020 2:17:43 AM, Error: OAuth 2 access token refresh failed. Client ID and secret sent in form body.. Response status code=InternalServerError. Response body:

 

 

Server Error in '/' Application.


The required anti-forgery cookie "__RequestVerificationToken" is not present.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.Mvc.HttpAntiForgeryException: The required anti-forgery cookie "__RequestVerificationToken" is not present.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

 [HttpAntiForgeryException (0x80004005): The required anti-forgery cookie "__RequestVerificationToken" is not present.] System.Web.Helpers.AntiXsrf.TokenValidator.ValidateTokens(HttpContextBase httpContext, IIdentity identity, AntiForgeryToken sessionToken, AntiForgeryToken fieldToken) +666 System.Web.Helpers.AntiXsrf.AntiForgeryWorker.Validate(HttpContextBase httpContext) +163 System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +156 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +859 System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +75 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +28 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +1295 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50 System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +75 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +532 System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +75 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +28 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +387 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +144 System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +75 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +28 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +320 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +2066 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +868 

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.36660

  • jseb04 Profile Picture
    jseb04 53 on at
    Re: Generic Oauth 2 connector not refreshing access or refresh tokens

    Did you ever resolved this? I am trying to connect an external api and i get the same issues. I have try to create it manually and by importing the json files. When i go and try to connect to it. i get the error

    {"error":"invalid_request","error_description":"Invalid redirect_uri: https://global.consent.azure-apim.net/redirect","rfc6749_reference":"4.1.2.1, 4.2.2.1"}
  • HEATFreight Profile Picture
    HEATFreight 1,020 on at
    Re: Generic Oauth 2 connector not refreshing access or refresh tokens

    Ah crap, I'm back again. I got the QuickBooks Online API apparently working perfectly in a custom connector, but I have another API that is returning the following error:

     

    Failed to refresh access token for service: oauth2. Correlation Id=..., UTC TimeStamp=..., Error: OAuth 2 access token refresh failed. Client ID and secret sent in form body.. Response status code=BadRequest. Response body: {"error":"invalid_grant"}

     


    It seems that something is happening with the refresh token request that is causing it to fail, but that happens in the background. We have no visibility into that request or the response, the OAuth2 custom connector handles that for you and it just works. Or so it did with the QBO API. This other API is more stubborn and I suspect it has something to do with the way they want their requests formatted. I'm not sure what Postman settings would affect this, but for instance these three settings in Postman's >Authorization tab seem like possible culprits:

     

    Add authorization data to [Request URL] vs. [Request Headers]

     

    or

     

    Authorize using browser [y/n]

     

    or

     

    Client Authentication [Send client credentials in body] vs. [Send as Basic Auth header]

     

     

    I am using the exact settings in the broken custom connector as what I used for the working QuickBooks Online custom connector, except with the base URL, auth URL, token URL, refresh URL, request URLs, client ID, and client secret all being unique to each API obviously. Other than those, everything else is exactly the same. API #2 works great for 20 minutes and then its access token expires and I get that error above^.

    Any Power Platform wizards out there? @RezaDorrani @shyamsu @v-bacao-msft 

  • HEATFreight Profile Picture
    HEATFreight 1,020 on at
    Re: Generic Oauth 2 connector not refreshing access or refresh tokens

    I'm having the same issue as @DerekH_AU although I have never been able to get the refresh token to work at all.

    I am building the Custom Connector by importing a Postman ver. 2.1 collection. Any tips?

    Edit: oh I think I found part of the answer here

    Edit2: Ok, yep I've figured it out! I exported a Postman v2.1 collection with all requests using the OAuth2 authorization method. Crucially, I left out the manual refresh token request from the collection. Postman requires you to build a manual request to keep the token refreshed because it will not do that for you even though it has that convenient "Get New Access Token" button: 

    HEATFreight_0-1639234343574.png

    You must leave out any manual authorization or refresh requests that you might use in Postman. These are not necessary (nor will they even work) in the custom connector. So you export a v2.1 Postman collection of just the non-authentication-related requests that you want to include in your connector. Each of these needs to have a type of "OAuth 2.0" in the Authorization tab. I followed this guide.

    You import the Postman collection into the Power Automate custom connector and proceed through the >General and >Security settings. Crucially, you must save the >Security settings before moving on. I never see this mentioned in tutorials, but if you don't do it, the client ID and secret as well as the Refresh URL will not be saved and you'll have to return to finish the >Security tab before you can actually "Update connector".

    The interesting thing here is that no matter what you do, the Refresh URl field will wipe itself as if it did not accept your input, but rest assured this is the expected behavior.

    Next tab is >Definition. The thing to remember about >Definition is that the conversion between the Swagger Editor and the Custom Connector user interface is broken. YOU CAN NOT TRUST IT TO CONVERT THE SWAGGER CODE PROPERLY! So here is what you do, follow the guide I linked above to edit the code in the Swagger Editor, and then click "Update connector" WHILE YOU STILL HAVE SWAGGER EDITOR TOGGLED ON! I never see any guides mention this part. If you switch back to the regular interface by toggling off the Swagger Editor, there is a chance something will break. You should be safe if you don't open any of the request parameters (like Path, Query, Headers, Body or any of their sub-fields), and clicking "Update connector" toggles off the Swagger Editor anyway, but definitely if you start opening any of those request parameter fields it will start breaking your Swagger code! I do think you can toggle off Swagger Editor before clicking "Update connector" and it will still work. It's mainly just opening the request parameters that begins to break the Swagger code.

    One example is if you try using integers as default values for string types. The normal Custom Connector interface will error out if you give a string parameter an integer for a default value (say if your API requires a company ID # parameter). If you get this "integer as string" error, you can simply open the Swagger Editor and put single quotes around the affected default value integers. If you click "Update connector" while Swagger Editor is still toggled on, or at least without navigating back to the string parameter in question, the single quotes will remain intact and your default value will populate in the >Test tab and wherever you use the connector, like Power Automate. But the second you open the string parameter in the normal Custom Connector interface (i.e. Swagger Editor toggled off), **BAM** no more single quotes. After opening the string parameter which has an integer default value, toggle Swagger Editor back on again and you will see that the single quotes have disappeared from around the default value integer.

    Off the top of my head, I'm not sure what other parts of the Swagger code breaks when you try editing the Custom Connector parmeters outside of the Swagger Editor, but suffice it to say something ain't right. Stick to the Swagger Editor and you should be fine.

    I'm at T+1 day of QuickBooks Online API Custom Connector keeping itself refreshed. The OAuth2 authorization code flow and refresh tokens work great in Custom Connectors if you do it right. 

  • Verified answer
    Derek H Profile Picture
    Derek H 21 on at
    Re: Generic Oauth 2 connector not refreshing access or refresh tokens

    The cookie error was being reported by the connector, when making connections without my browser even being open. In any case, for now, I have recreated the connection the connector is using and so far I have had 24 hours of refreshed access tokens, making a request every 15 minutes, when the access token expires after 20 mins. For the moment, recreating the connection seems to have resolved the issue.

  • shyamsu Profile Picture
    shyamsu on at
    Re: Generic Oauth 2 connector not refreshing access or refresh tokens

    This appears to be a client/browser side issue. We require cookies to be enabled. Can you please make sure you doing this all in one browser with cookies allowed?

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,343

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard

Featured topics