function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ravinder singh 95ravinder singh 95 

Salesforce Wordpress Integration getting forbidden error , status code 403

I am trying to get Post from wordpress and store into Salesforce Account.

Note : CALLOUT_RESPONSE|[29]|System.HttpResponse[Status=Forbidden, StatusCode=403]

I am getting 403 status code.

public with sharing class WordpressIntegration {
   // Created a remote site setting for this Rest API
    //public List <JSONWrapper> listWrapper {get;set;}
    public List <JSONWrapperWordpess> listWrapper {get;set;}
    List<Account> lstAcc = new List<Account>();
    public String accessToken = 'YWRtaW46QW1hemluZ0AxMjM=';
    public WordpressIntegration() {
        listWrapper = new List < JSONWrapperWordpess >();
    }
    public void fetchDataFromExternalSystem(){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        String username = 'admin';
        String password = 'Amazing@123';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
        
        //req.setEndPoint('https://api.github.com/users/hadley/orgs');
        req.setEndPoint('http://localhost/project1/wp-json/wp/v2/posts?_fields=author,id,excerpt,title,link');
        req.setMethod('GET');
        HTTPResponse res = h.send(req);
        System.debug('res.getStatusCode() ###'+res.getStatusCode());
        //JSONParser parser = JSON.createParser(res.getBody());
        if (res.getStatusCode() == 200) {
            listWrapper = (List<JSONWrapperWordpess>)System.JSON.deSerialize(res.getBody(), List<JSONWrapperWordpess>.class);
            /*
            If the response contains only one value instead list, then you can use the below code
            JSONWrapper obj = (JSONWrapper) JSON.deSerialize(res.getBody(), JSONWrapper.class); 
            listWrapper.add(obj);
            */
            System.debug('listWrapper @@'+ listWrapper);
            for(JSONWrapperWordpess a : listWrapper){
                Account aa = new Account();
                aa.Name = a.title;
                aa.DOB__c = date.newInstance(2012,05,22);
                aa.Site = a.link;
                aa.AccountNumber = a.id;
                lstAcc.add(aa);
            }
            insert lstAcc;
        }
    }
    public class JSONWrapperWordpess {
        public String title {get;set;}
        public String id {get;set;}
        public String link {get;set;}
        
    }
}
Best Answer chosen by ravinder singh 95
scottbcovertscottbcovert
I see the endpoint is set to localhost - the callout is being made from a Salesforce server so there needs to be a viable internet route between Salesforce and the callout endpoint; you won't be able to test this until your Wordpress site is hosted publicly. This issue doesn't apply to a callout from Postman since that originates from your own machine so it can trace the localhost call properly.

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Ravinder, 

Does your user have the API Enabled permission?

Also, you can try replacing your endpoint with a dummy endpoint (Obtain your endpoint from https://hookbin.com/) and make the same call. This will help confirm if the problem is with endpoint

Anudeep


 
ravinder singh 95ravinder singh 95
Yes my user at Salesforce end is API eabled. Also if I hit the wodpress endpoint  using POSTMAN then I am geetting the response.
 
scottbcovertscottbcovert
I see the endpoint is set to localhost - the callout is being made from a Salesforce server so there needs to be a viable internet route between Salesforce and the callout endpoint; you won't be able to test this until your Wordpress site is hosted publicly. This issue doesn't apply to a callout from Postman since that originates from your own machine so it can trace the localhost call properly.
This was selected as the best answer
tanmay sktanmay sk
The same issue with my site http://techstockinsights.com/ I somehow found your answer it's working now.
Chery SparkChery Spark
From the error message it seems, the issue is due to misconfiguration during the setup side.

To be frank I don't know exactly what are the steps to be taken for setting up this integration with WordPress and salesforce.
You have added the localhost in the salesforce. Your local site is not reachable through the internet. It will throw errors for sure. You can use NGROK (https://ngrok.com/) to connect your local server to the internet. 
You need to add the URL where you are making the API  calls to “remote sites” in the salesforce. Check this link for more information [ https://help.salesforce.com/s/articleView?id=sf.configuring_remoteproxy.htm&type=5 (https://help.salesforce.com/s/articleView?id=sf.configuring_remoteproxy.htm&type=5)]
Hope my answer helps many people like us…