• tanmay sk
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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;}
        
    }
}