Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Unable to create node at XXX

Avatar

Level 2

Hi

I am new to AEM, I created a AEM servlet and try to call a third party API to get a Oauth Token

 

 

@SlingServlet(paths={"/bin/test01"},methods= {"POST"},metatype=false)
public class OauthAuthServlet extends SlingAllMethodsServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -4325654356300019990L;
	
	public static String OAUTH_SERVER_URL = "https://dm-us.informaticacloud.com/authz-service/oauth/token";
	@Override
    protected void doGet( SlingHttpServletRequest request,  SlingHttpServletResponse response) {
    	
		this.doPost(request, response);  	
    }
	
	@Override
    protected void doPost( SlingHttpServletRequest request,  SlingHttpServletResponse response){
    	String cc= request.getParameter("CLIENT_CREDENTIALS");
    	String token = null;
//    	try {
//			response.getWriter().write(cc);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 	
    	OkHttpClient client = new OkHttpClient();
    	String authHeader = "Basic " + cc;

		Request okrequest = new Request.Builder()
				.url(OAUTH_SERVER_URL + "?grant_type =client_credentials")
				.method("POST", RequestBody.create(MediaType.parse("text/plain"), ""))
				.addHeader("Authorization", authHeader)
				.build();

		Response okresponse;
		try {
			okresponse = client.newCall(okrequest).execute();
			Map <String, Object> jsonResponse = new Gson().fromJson(okresponse.body().string(), Map.class);
			token = (String)jsonResponse.get("access_token");
			response.getWriter().write(token);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 	}

 

 

 and I get this error:

 

 

org.apache.sling.api.resource.PersistenceException: Unable to create node at
						/bin/test01

 

 

 

Can anyone give me some advises? Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @HanL1 

 

On which instance you are trying to execute this servlet author, publisher or dispatcher? I would suggest you to use ClosableHttpClient to execute the post request instead of OkHttpClient as it's jar isn't compatible with AEM and not able to import in AEM bundles.

 

Refer to the code snippet that's been provided in your other post where you were trying with CloseableHttpClient:-

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/closeablehttpclient-can-t-... 

 

Hope this helps.

View solution in original post

6 Replies

Avatar

Community Advisor

Please check the solution given here

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unable-to-create-resource-...

 

If your servlet path is /bin/servlets then you need to update the dispatcher rule for publish env as below:

Filter rule needs update if not done already as it might be blocking.

{ /type "allow" /method "GET" /url "/bin/servlets*" } in filter.any

Avatar

Correct answer by
Community Advisor

Hi @HanL1 

 

On which instance you are trying to execute this servlet author, publisher or dispatcher? I would suggest you to use ClosableHttpClient to execute the post request instead of OkHttpClient as it's jar isn't compatible with AEM and not able to import in AEM bundles.

 

Refer to the code snippet that's been provided in your other post where you were trying with CloseableHttpClient:-

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/closeablehttpclient-can-t-... 

 

Hope this helps.

Avatar

Community Advisor

Your servlet is not resolved, please check using http://localhost:4502/system/console/servletresolver 

 



Arun Patidar