Problem Token ID JAVA JDK "Method Not Allowed"

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("CLIENTID_XXXXXXXXXXXX", "SECRET_XXXXXXXXXXXXXXXXXX");
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("grant_type", "client_credentials");

	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

There's a couple issues I can see:

  1. The request must be a POST. Use the HttpPost class instead.
  2. grant_type=client_credentials is the form data (request body) content, not a header.

Hi Tim,

Thank you very much for your answer, previously I tried with Post / Get but always I used the Grant_Type as header.
Now I changed to Post Method and Grant_Type 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("grant_type=client_credentials");
	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

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.