5
votes

I am unable to get an access token when trying to get one from SalesForce. I entered a callback URL of https://localhost:8081/AppCallback. However, I get null in my browser when I print out the token. When I change the consumer key value I still get success, it doesn't seem like I'm hitting the service. Here is my configuration. I send a request to http://localhost:8081/authorize and I get back

You have successfully authorized the connector. Your access token id is:null

   <http:listener-config name="HTTP_Listener_Configuration"
            host="localhost" port="8081" doc:name="HTTP Listener 
    Configuration" />

        <sfdc:config-with-oauth name="salesforce-oauth"
            onNoToken="EXCEPTION"
       accessTokenUrl="https://app.my.salesforce.com/services/oauth2/token"

     authorizationUrl="https://app.my.salesforce.com/services/oauth2/authorize"
            consumerKey="myConsumerKey"
            consumerSecret="33388E8E" doc:name="Salesforce (OAuth)">
            <sfdc:oauth-callback-config domain="localhost"
                localPort="8081" remotePort="8081" path="AppCallBack" />
        </sfdc:config-with-oauth>

   <flow name="authorize" doc:name="authorize">
            <http:listener config-ref="HTTP_Listener_Configuration"  allowedMethods="GET" 
                path="authorize" doc:name="HTTP">
                <http:response-builder statusCode="200" reasonPhrase="You have successfully authorized the connector. Your access token id is: #[flowVars.OAuthAccessTokenId]" />
                <http:error-response-builder statusCode="404" reasonPhrase="An error has occurred authorizing the connector" />
            </http:listener>
            <sfdc:authorize immediate="TRUE" config-ref="salesforce-oauth"  display="PAGE" doc:name="Authorize at Salesforce" />
        </flow>

  <flow name="sfdctestFlow1" doc:name="readContactFlow1">
            <http:listener config-ref="HTTP_Listener_Configuration"
                path="run" doc:name="HTTP" />
            <sfdc:query config-ref="salesforce-oauth" accessTokenId="#[flowVars.OAuthAccessTokenId]"
                doc:name="Perform a query at Salesforce" query="SELECT id,lastname,lastmodifieddate from contact limit 10" />
            <foreach doc:name="For Each">
                <logger doc:name="Logger" level="INFO" message="contact: #[payload]" />
            </foreach>
            <set-payload doc:name="Set Payload to formatted output"
                value="Salesforce query returned #[payload.size()] contacts." />
            <logger doc:name="Logger" level="INFO" message="contact: #[payload]" />
        </flow>

What am I doing wrong? Why isn't the Salesforce validating and returning an access token? How am I supposed to use the callback URL?

1
Are you using Mule 4 for this connectivity? Did you try the Salesforce connectors?Lijin

1 Answers

0
votes

you need to use the Salesforce connector, it's very easy with that. And considering the current approach you tried, there is no username, and a password is provided.

If you in the below code(copied from the post) -

  1. Access Token URL - It is correct(and I assume you're hitting directly to the prod).
  2. Consumer Key - It is correct.
  3. Consumer Secret - It is correct
  4. Username - Not present
  5. Password - Not present. When you try with password make sure that you append security toekn(if enabled)
  <sfdc:config-with-oauth name="salesforce-oauth"
            onNoToken="EXCEPTION"
       accessTokenUrl="https://app.my.salesforce.com/services/oauth2/token"

     authorizationUrl="https://app.my.salesforce.com/services/oauth2/authorize"
            consumerKey="myConsumerKey"
            consumerSecret="33388E8E" doc:name="Salesforce (OAuth)">
            <sfdc:oauth-callback-config domain="localhost"
                localPort="8081" remotePort="8081" path="AppCallBack" />
        </sfdc:config-with-oauth>