0

I am developing Spring MVC + Apache Oltu + Salesforce Integration example. In this example, I have created connected App in the salesforce as shown below:

In this example I am using below:

  • ACCESS_TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
  • AUTHORIZATION_URL = "https://login.salesforce.com/services/oauth2/authorize";

What will be the OAuthBearerClientRequest Request ? Like for Github I had used https://api.github.com/user. What will be the URL for the salesforce ?

For ex: OAuthBearerClientRequest for

  • Linkedin - https://api.linkedin.com/v1/people/~?oauth2_access_token=
  • Facebook - https://graph.facebook.com/me/friends
  • Github- https://api.github.com/user

What is for the salesforce ?

This URL also dont have info what I want: https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_an_Access_Token_using_a_SAML_Bearer_Assertion.

enter image description here

Please let me know if someone needs code for reference.

enter image description here

The error which I see::

error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration

Here is the code

import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthResourceResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/salesforce")
public class SalesforceController {
    private static final Logger logger = LoggerFactory.getLogger(SalesforceController.class);

    private static final String AUTHORIZATION_URL = "https://login.salesforce.com/services/oauth2/authorize";
    private static final String CLIENT_ID = "3MVG9d8..z.hDcPJZPIzGJ5UZDqJOJY.3R6RBM8sJAF8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    private static final String CLIENT_SECRET = "37506609XXXXXXXXX";
    private static final String REDIRECT_URL = "http://localhost:8080/apache-oltu/instagram/redirect";
    private static final String ACCESS_TOKEN_URL = "https://login.salesforce.com/services/oauth2/token";

    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    public String authenticate() throws OAuthSystemException {
        OAuthClientRequest request = OAuthClientRequest
                .authorizationLocation(AUTHORIZATION_URL)
                .setClientId(CLIENT_ID)
                .setRedirectURI(REDIRECT_URL)
                .setResponseType("code")
                .setScope("chatter_api")
                .buildQueryMessage();

        System.out.println("REDIRECT TO: "+request.getLocationUri());
        return "redirect:" + request.getLocationUri();
    }



    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public HttpEntity<String> redirect(
            @RequestParam(value = "code", required = false) String code) throws OAuthSystemException, OAuthProblemException {
        String value = "UNKNOWN";

        if (code != null && code.length() > 0) {
            System.out.println("Received CODE: "+code);
            String details = getAccessToken(code);
            value = details;
        }

        return new ResponseEntity<String>(value,HttpStatus.OK);
    }

    private String getAccessToken(String authorizationCode) throws OAuthSystemException, OAuthProblemException {
        OAuthClientRequest request = OAuthClientRequest
                .tokenLocation(ACCESS_TOKEN_URL)
                .setGrantType(GrantType.AUTHORIZATION_CODE)
                .setClientId(CLIENT_ID)
                .setClientSecret(CLIENT_SECRET)
                .setRedirectURI(REDIRECT_URL)
                .setCode(authorizationCode)
                .buildBodyMessage();

        //create OAuth client that uses custom http client under the hood
        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

        GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);

        System.out.println(oAuthResponse.getBody());

        request= new OAuthBearerClientRequest("https://ap5.salesforce.com/services/apexrest/Cases").
                setAccessToken(oAuthResponse.getAccessToken()).
                buildQueryMessage();


        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthResourceResponse resourceResponse= client.resource(request, "GET", OAuthResourceResponse.class);

        if (resourceResponse.getResponseCode()==200){           
            logger.debug("HTTP OK");
            System.out.println(resourceResponse.getBody());
            return resourceResponse.getBody();
        }
        else{
            System.out.println("Could not access resource: " + resourceResponse.getResponseCode() 
            + " " + resourceResponse.getBody());
            return null;
        }
    } 
}

1 Answer 1

0

I remember I had the same issue when just trying to connect.
Back then, there was an issue with the ACCESS_TOKEN_URL.

Try changing the URL to https://ap5.salesforce.com/services/oauth2/token (to specify the specific instance), this is what fixed it for me back then.

2
  • Still its not working after successful login into the salesforce when its redirecting back to the This program @RequestParam(value = "code", required = false) String code has null values. This should have a proper value. Any reason to fixed this ? Commented Oct 2, 2017 at 14:08
  • @Rahul Sharma - Any help ?
    – Ash_P
    Commented Oct 2, 2017 at 18:21

You must log in to answer this question.