Unable to create node at XXX | Community
Skip to main content
Level 2
December 15, 2022
Solved

Unable to create node at XXX

  • December 15, 2022
  • 3 replies
  • 3794 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Avinash_Gupta_

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-execute-in-aem/td-p/563499 

 

Hope this helps.

3 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
December 15, 2022

Please check the solution given here

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unable-to-create-resource-named-servlets-in-bin/m-p/456341

 

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

HanL1Author
Level 2
December 16, 2022

Thank you for your answer, I will check.

Avinash_Gupta_
Community Advisor and Adobe Champion
Avinash_Gupta_Community Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
December 16, 2022

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-execute-in-aem/td-p/563499 

 

Hope this helps.

HanL1Author
Level 2
December 16, 2022

Thank you, I will change it.

arunpatidar
Community Advisor
Community Advisor
December 16, 2022

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

 

Arun Patidar
HanL1Author
Level 2
December 16, 2022

Thank you, I will check.