Jorge_Contreras | 2018-05-30 14:10:56 UTC | #1
Hi,
This is my first post and I hope that you can helpme. I can't create a token ID, Previously I worked with API purecloud and Visual Studio C# and I created a token without problems and easy.
Example C#:
var accessTokenInfo = Configuration.Default.ApiClient.PostToken("CLIENTIDXXXXXXXXXXXX", "SECRETXXXXXXXXXXXXXXXXXX"); Console.WriteLine("Access token=" + accessTokenInfo.AccessToken); var token = accessTokenInfo.AccessToken; Configuration.Default.AccessToken = token;
Actually I working with API purecloud and Java SDK, but I can´t create a token ID. I looked the Purecloud Documentation and Developer Forum but i can´t find the solution. i looked examples base64 encode in https://developer.mypurecloud.com/api/rest/authorization/base-64-encoding.html and https://developer.mypurecloud.com/api/rest/client-libraries/java/index.html
I'm working with a manual token (in C# created) for execute many API Purecloud in java, I tried many solution but my problem continues. this is my actual code in JAVA SDK.
Return: "Method Not Allowed"
public void getURL() throws UnsupportedEncodingException {
String clientid ="XXXXXXXXXXXX-XXXXXXXXXXXXXX-"; String clientSecret = "XXXXXXXXXXXXXXXxxxxxXXXXXXXXX"; Base64 base64 = new Base64();
String encodeData = new String(Base64.encodeBase64((clientid+":"+clientSecret).getBytes("ISO-8859-1"))); System.out.println(encodeData);
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet post = new HttpGet("https://login.mypurecloud.com/oauth/token"); post.setHeader("Authorization", "Basic" +encodeData ); post.setHeader("Content-Type","application/x-www-form-urlencoded"); //post.setHeader("granttype", "clientcredentials");
JSONObject json = new JSONObject(); json.put("", ""); // json.put ... // Send it as request body in the post request
StringEntity params = null;
try { params = new StringEntity(json.toString()); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpParams parametrosList; //parametrosList.setParameter(name, value); //post.setParams(prametrosList);
try { CloseableHttpResponse response = httpclient.execute(post); HttpEntity f = response.getEntity(); String salida = EntityUtils.toString(response.getEntity(), Charset); System.out.println(salida);
} catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpclient.getConnectionManager().shutdown(); }
}
Is there another way?
best regards Jorge
tim.smith | 2018-05-30 21:09:44 UTC | #2
There's a couple issues I can see:
- The request must be a POST. Use the
HttpPost class instead. grant_type=client_credentials is the form data (request body) content, not a header.
Jorge_Contreras | 2018-05-31 13:50:48 UTC | #3
Hi Tim,
Thank you very much for your answer, previously I tried with Post / Get but always I used the GrantType as header. Now I changed to Post Method and GrantType as body and I have the token ID.
String clientid ="XXXXXXXXX-XXXXXX-XXX-XXXX-XXXXXXXX"; String clientSecret = "X-XGXXXXXXXXXZXXXXXXXXXX"; String message = (clientid+":"+clientSecret); Base64 base64 = new Base64();
String encodeData = new String(Base64.encodeBase64((clientid+":"+clientSecret).getBytes("ISO-8859-1"))); System.out.println(encodeData);
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost post = new HttpPost("https://login.mypurecloud.com/oauth/token"); post.setHeader("Authorization", "Basic " +encodeData ); post.setHeader("Content-Type","application/x-www-form-urlencoded"); StringEntity params=new StringEntity("granttype=clientcredentials"); post.setEntity(params);
JSONObject json = new JSONObject(); json.put("", ""); // json.put ... // Send it as request body in the post request
try { params = new StringEntity(json.toString()); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpParams parametrosList; //parametrosList.setParameter(name, value); //post.setParams(prametrosList);
try { CloseableHttpResponse response = httpclient.execute(post); HttpEntity f = response.getEntity();
String salida = EntityUtils.toString(response.getEntity(), Charset); System.out.println(salida);
} catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpclient.getConnectionManager().shutdown(); }
best regards Jorge
system | 2018-07-01 13:50:50 UTC | #4
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 2932