Prateek created OLTU-216:
----------------------------
Summary: How to set the query parameter the apache oltu
OAuthClientRequest or OAuthBearerClientRequest?
Key: OLTU-216
URL: https://issues.apache.org/jira/browse/OLTU-216
Project: Apache Oltu
Issue Type: Bug
Reporter: Prateek
How to set `query` parameter in the **Apache Oltu** ?
{code:java}
// Set the access Token in the header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer
"+oAuthResponse.getAccessToken());
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
// create HTTP Entity
HttpEntity<?> entity = new HttpEntity<>(headers);
//create Uri Component Builder
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl("https://ap5.salesforce.com/services/data/v40.0/query")
.queryParam("q", "SELECT Id, Name FROM Contact
WHERE Name='Chris Smith'");
// RestTemplate
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> resourceResponse =
restTemplate.exchange(builder.build().encode().toUri(),
HttpMethod.GET,entity,String.class);
System.out.println("===============================================================");
System.out.println("RESULT :: "+resourceResponse.getBody());
{code}
Now when I am trying to used the **Apache Oltu API**, I see the problem in how
to set the query parameters.
I developed the below code, but there I am facing the below issue:
{code:java}
OAuthClientRequest request = OAuthClientRequest
.tokenLocation(ACCESS_TOKEN_URL)
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setRedirectURI(REDIRECT_URL)
.setCode(authorizationCode)
.buildQueryMessage();
//create OAuth client that uses custom http client under the hood
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthAccessTokenResponse oAuthResponse =
oAuthClient.accessToken(request, OAuth.HttpMethod.POST);
//
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl("https://ap5.salesforce.com/services/data/v40.0/query")
.queryParam("q", "SELECT Id, Name FROM Contact WHERE
Name='Chris Smith'");
request= new
OAuthBearerClientRequest("https://ap5.salesforce.com/services/data/v41.0/").
setAccessToken(oAuthResponse.getAccessToken()).
buildBodyMessage();
request.setLocationUri(builder.build().encode().toString());
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;
}
}
{code}
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)