Mule OAuth2 using Client Credentials as grant_type

2019-03-06 18:13发布

My requirement is to use client_credentials grant_type of OAuth2 to obtain the access token in Mule. I want to implement a OAuth enabled custom connector. I am unable to achieve it using following configuration:

<XXX-auth2:config name="TestAuth" consumerKey="abc" consumerSecret="1234" doc:name="TestAuth">
        <XXX-auth2:oauth-callback-config domain="localhost" localPort="8082" path="callback" remotePort="8082" async="false"/>
    </XXX-auth2:config>
    <flow name="testFlow1" doc:name="testFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082"  path="testoauth" doc:name="HTTP" />
         <XXX-auth2:authorize config-ref="TestAuth" doc:name="TestAuth"/>
        <logger message="Test Auth output:#[flowVars['tokenId']]" level="INFO" doc:name="Logger"/>
    </flow>

My requirement is to manage OAuth protected calls from a batch job. Please suggest.

1条回答
我命由我不由天
2楼-- · 2019-03-06 18:33

It would help if you share your connector code.

However if you are using client-credentials flow, using the @OAuth2 annotations is probably not the way to go as this uses HTTP GET to redirect to the service provider.

As this grant type doesn't require redirection or callbacks, typically you just pass the credentials in as basic auth or in the POST body to a token endpoint.

You're better off using connection management features of Devkit and using a http client to call out to your token endpoint and store the token endpoint in the @Connect method:

http://www.mulesoft.org/documentation/display/34X/Implementing+Connection+Management

private String token;
@Connect
    public void connect(@ConnectionKey String clientKey, @Password String clientSecret)
            throws ConnectionException
    {
        //HTTP client to call token endpoint:
        //curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=client_credentials'
        this.token = "extract from response"
    }
查看更多
登录 后发表回答