'Mule 4 HTTP Request Client to access OAuth 2 end point

There is an external service (lets say "https://external-service.com/service") which is secured by OAuth2. I have client ID (Lets say "123_my_client_id"), Secret ID ("324_mysecret") and the access token URL ( lets say "https://access-token.com/access-token") which returns me the token.
I want to access this service with my Mule 4 Http Request. I followed this https://docs.mulesoft.com/connectors/http/http-authentication#oauth2-client-credentials, but couldn't find any workable solution.

This can be done in Mule 3.9. but still struggling to set up this http request configuration for Mule 4. Can anyone please help to setup this request config .



Solution 1:[1]

In order to migrate the request authentication to Mule 4,the config now belongs in the http:request-connection component and the HTTP authentication configuration must be placed within an http:authentication component. This applies to all authentication types supported: basic, digest, NTLM and OAuth2.

From the example in the link provided:

    <http:request-config name="HTTP_Request_Configuration"
                     host="some.api.com" port="80" basePath="/api/1.0">
        <oauth:client-credentials-grant-type
            clientId="your_client_id" clientSecret="your_client_secret"
            tokenUrl="http://some.api.com/api/1.0/oauth/token"
            scopes="access_user_details, read_user_files">
        </oauth:client-credentials-grant-type>
    </http:request-config>

Changes to something like:

    <http:request-config name="HTTP_Request_Configuration">
        <http:request-connection host="some.api.com" port="80">
            <http:authentication>
                <oauth:client-credentials-grant-type
                    clientId="your_client_id" clientSecret="your_client_secret"
                    tokenUrl="http://some.api.com/api/1.0/oauth/token" scopes="access_user_details, read_user_files" />
            </http:authentication>
        </http:request-connection>
    </http:request-config>

Studio may complain about the oauth element, but it should start up fine. Just ignore it.

Solution 2:[2]

To anyone wondering how to do that through the user interface, it is pretty easy but the mulesoft documentation could be more clear. I ended up finding out how to do it, here's the step-by-step process :

  1. create your HTTP Request processor
  2. choose the display name you want in "Display Name:"
  3. choose the desired method in the "Method:" dropdown menu (ex. : GET)
  4. set the path to the endpoint you wish to reach in "Path:"
  5. then create a HTTP_Request_Configuration, by clicking the "+" on the right of "Configuration:" in the "Basic Settings" box
  6. In the configuration window, set up the protocol (HTTP or HTTPS)
  7. In the configuration window, set up the port (80 for HTTP and 443 for HTTPS)
  8. In the configuration window, set up the host (ex. : www.url-to-request.com)
  9. Scroll down to find the "Authentication:" dropdown menu
  10. Choose "Client credentials grant type"
  11. Set up you client-id and client-secret
  12. Set up scopes if required by the application provider
  13. And finally set up the "Token url" (url given by the application provider to retrieve the oauth identification token)
  14. Click OK

After doing this, your oauth2 request should be working. As said in the mulesoft documentation, each time you run the project, a token will be automatically generated. You then can execute as many request as you want without worrying about the authentication anymore.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan Carter
Solution 2 Henry Mont