You need to sign in to do that
Don't have an account?
ravinder 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;}
}
}
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;}
}
}
All Answers
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
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…