You have created a connected app with the type "App acts on its own behalf (client credentials)" and using the client id and the secret to generate a token to hit an Anypoint endpoint. And you get the below "403 Unauthorized":
GET https://anypoint.mulesoft.com/apiplatform/repository/v1/organizations/{{organisation_id}}/environments { "name": "AuthorizationError", "message": "Unauthorized" }
The cause of the issue is because depending on how the API is designed and what type of authorization it supports, as in this case this API is a protected resource controlled by the service provider. In the case of this API https://anypoint.mulesoft.com/apiplatform/repository/v1/organizations/{{organisation_id}}/environments, it does not support client credential grant type and you have to use authorization code grant type while generating the token for accessing the API.
Hence, You must create the connected app type "App acts on behalf of a user" with grant type "Authorization Code" as explained in the link.
Callback URL(same as redirect url in connected app, this is the url to redirect after authorization): https://oauth.pstmn.io/v1/callback Authorize using browser Auth URL https://anypoint.mulesoft.com/accounts/api/v2/oauth2/authorize Access Token URL https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token Client ID <from connected app> Client Secret. <from connected app> Scope. full
For EU plane, please use
Callback URL(same as redirect url in connected app, this is the url to redirect after authorization): https://oauth.pstmn.io/v1/callback Authorize using browser Auth URL https://eu1.anypoint.mulesoft.com/accounts/api/v2/oauth2/authorize Access Token URL https://eu1.anypoint.mulesoft.com/accounts/api/v2/oauth2/token Client ID <from connected app> Client Secret. <from connected app> Scope. full
Click Proceed and you will see the token generated
When the above is confirmed that's working you can put all these in a Mule app hence the concept of a "connected app" so it could be used as a regular Mule app. Please use the attached test Anypoint Studio project called amcall_new.jar and review the src/main/resource/local-config.yaml within the project:
anypoint: base.path: "/apiplatform/repository/v2/organizations/<your_org_id>" host: "gov.anypoint.mulesoft.com" port: "443" local.callback.path: "/callback" external.callback.url: "http://localhost:8070/callback" local.authorization.url: "http://localhost:8070/login" authorization.url: "https://gov.anypoint.mulesoft.com/accounts/api/v2/oauth2/authorize" client.id: "connected_app_client_id" client.secret: "connected_app_client_secret" token.url: "https://gov.anypoint.mulesoft.com/accounts/api/v2/oauth2/token"
1. Fill in your org id, connected app's client id and secret from Anypoint Platform's Access Management. Save the app and run it
2. In a browser call http://localhost:8070/login to retrieve the access token
3. In another browser tab call http://localhost:8070/amcall to invoke the call to the API to get the desired response
Please note depending on if you are running locally or as a Cloudhub app please modify the below in your app
anypoint: base.path: "/accounts/api/organizations/erere-a992-4e79-a29d-a9e975e0de5e/environments" host: "anypoint.mulesoft.com" port: "443" local.callback.path: "/callback" external.callback.url: "http://localhost:8070/callback" local.authorization.url: "http://localhost:8070/login" authorization.url: "https://anypoint.mulesoft.com/accounts/api/v2/oauth2/authorize" client.id: "<CLIENT ID FROM CONNECTED APP>" client.secret: "<CLIENT SECRET FROM CONNECTED APP>" token.url: "https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token"
redirect URL http://localhost:8070/callback
anypoint: base.path: "/accounts/api/organizations/erere-a992-4e79-a29d-a9e975e0de5e/environments" host: "anypoint.mulesoft.com" port: "443" local.callback.path: "/callback" external.callback.url: "http://<APP URL>/callback" local.authorization.url: "http://<APP URL>/login" authorization.url: "https://anypoint.mulesoft.com/accounts/api/v2/oauth2/authorize" client.id: "<CLIENT ID FROM CONNECTED APP>" client.secret: "<CLIENT SECRET FROM CONNECTED APP>" token.url: "https://anypoint.mulesoft.com/accounts/api/v2/oauth2/token"
redirect URL http://<APP URL>/callback
How to create connected apps creation using API
Creating connected app and getting the bearer token example
How to deploy an application to Cloudhub using Connected App functionality
001120847

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.