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
Solved! Go to Solution.
Views
Replies
Total Likes
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:-
Hope this helps.
Please check the solution given here
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
Thank you for your answer, I will check.
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:-
Hope this helps.
Thank you, I will change it.
Your servlet is not resolved, please check using http://localhost:4502/system/console/servletresolver
Thank you, I will check.
Views
Likes
Replies
Views
Likes
Replies