Below is my code to servlet
Views
Replies
Total Likes
Hi,
Clearly, the issue lies with the token. Please debug and validate that the Authorization header is correctly formed, and confirm that the token you're using is valid. You can try hitting the author instance using the token via Postman or another tool to ensure it’s working properly.
Please check this example: https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-with-aem-headles...
Hope this helps!
Views
Replies
Total Likes
Hi @EstebanBustamante ,
I tested it using the PostMan and standalone Main Program and it looks to be working fine. But when hitting with publisher its getting 401 and when having token in the logs we copied it and through that token only we hit the servlet throygh postman, it worked but don't know what the problem when calling it through this code through publisher??
Views
Replies
Total Likes
Is this working locally?
Views
Replies
Total Likes
yes
Views
Replies
Total Likes
Hi @bhavigoyal ,
401 unauthorised error suggest token is expired or not valid(malformed/null).
Either debug or just log the value of token after statement token = dcpUpdateUserService.getToken();
Just make sure in general token have expiration time so hopefully service would be either generating token each time or would be regenerating before token get expired.
Thanks
done this and token is valid and dont expires.
Views
Replies
Total Likes
Hi @bhavigoyal,
I don't see anything wrong with your code example. Sometimes when facing such weird issues it is best to try another option and compare the results.
If you have checked all the other options my colleagues suggested, then I suggest you try using a simple POST request to the OTTB POST Servlet. If that is working fine then you can compare the two requests and check what is difference. If it's not working, then we can conclude the issue is not with your code but somewhere on the network/firewall level.
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import javax.servlet.Servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Component(
service = Servlet.class,
property = {
"sling.servlet.paths=/bin/example/updateComponent",
"sling.servlet.methods=GET"
}
)
public class UpdateComponentPropertyServlet extends SlingAllMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
// Path to the component on the Author instance
String componentPath = "/content/my-site/en/jcr:content/my-component";
String authorUrl = "https://author.example.com" + componentPath;
// Authorization token (use a secure mechanism to retrieve this)
String authorizationToken = "Bearer YOUR_TOKEN_HERE";
// HTTP client
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost postRequest = new HttpPost(authorUrl);
// Set headers
postRequest.setHeader("Authorization", authorizationToken);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Set parameters for the Sling POST servlet
List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("./myProperty", "newValue")); // Update "myProperty" to "newValue"
params.add(new BasicNameValuePair(":operation", "modify")); // Required operation for the POST servlet
postRequest.setEntity(new UrlEncodedFormEntity(params));
// Execute request
httpClient.execute(postRequest);
response.getWriter().write("Component property updated successfully.");
} catch (Exception e) {
response.getWriter().write("Failed to update component property: " + e.getMessage());
}
}
}
In the lack of a better advice, I hope this helps,
Daniel
Views
Replies
Total Likes
It Is Amazing.
Views
Replies
Total Likes
Views
Likes
Replies