+ Start a Discussion
Anindya ChattopadhyayAnindya Chattopadhyay 

Calling Rest API from WordPress site

Hi there:

I am really new to this big Salesforce world. One of the requirement for us is to create Salesforce entites remotely from our WordPress site. The only way I can think of right now is to use JavaScript.

Can anyone please tell me how to do it using REST API? I found some documentation on https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit. However I am not sure how to approach here. What values do I need to supply for var clientId , var redirectUri and var proxyUrl? Is there any other way?

Any help will be much appreciated.

Thanks
Anindya
Best Answer chosen by Anindya Chattopadhyay
Sathish balajiSathish balaji
1. Yes callback URL is the one from where you had made the initial request from. Note that you can provide a complete different URL here in order for you users to navigate to a different page on successfull authentication.

2. When you have any intermediate proxy servers in your organization, and users use the proxy in web browser, request goes via that proxy server to internet. In this case you may need to pass on the proxy server's URL in that parameter. If you dont have any proxy servers, then you shall not pass the proxyURL parameter and it's an optional one. 

Let me know if it helps

All Answers

Sathish balajiSathish balaji
Hi Anindya,

Basically you need an access_token with every request to get salesforce data. In order to obtain one, you need to first authenticate with your Salesforce organization. This can be done through OAuth, which is recommended way instead of hardcoding credentials.

You must create a 'Connected app' in salesforce by registering your wordpress application details. By doing so salesforce creates a Client ID and Client Secret for you application. When you first authenticate with salesforce, provide these 2 values along with the request. This will redirect you to Salesforce authentication page where, upon successful authentication, you will be redirected to a web page which you mentioned in 'redirectURI' parameter(This can be you initial page where you made the authentication request in you application).Once redirected, you'll have an additional parameter in the response, authorization code. 

Checkout the documentation below for a detailed walkthrough.

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm

Best regards,
Sathish Balaji
Anindya ChattopadhyayAnindya Chattopadhyay

Hi Sathish:

Thanks. I got some idea after going through that. I also found the following link partciularly useful:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm

So we can either use OAuth or Session ID, the later one requiring username and password.

Couple of more queries:

1. Does the callback URL relate to the external web page from where I am calling the Salesforce API using JavaScript?

2. I am still not sure what's proxyUrl?

I believe if I need to write managed code (Java, C# etc.) to build a client to consume the API, I don't need the proxyUrl?

I hope I understood it right. Any help will be much appreciated.

Thanks and regards,
Anindya



 

Sathish balajiSathish balaji
1. Yes callback URL is the one from where you had made the initial request from. Note that you can provide a complete different URL here in order for you users to navigate to a different page on successfull authentication.

2. When you have any intermediate proxy servers in your organization, and users use the proxy in web browser, request goes via that proxy server to internet. In this case you may need to pass on the proxy server's URL in that parameter. If you dont have any proxy servers, then you shall not pass the proxyURL parameter and it's an optional one. 

Let me know if it helps
This was selected as the best answer
Anindya ChattopadhyayAnindya Chattopadhyay
Thanks. This very line:
var client = new forcetk.Client(clientId, loginUrl, proxyUrl);
made me think that proxyUrl is mandatory. So in my case, the above line will look like this:
var client = new forcetk.Client(clientId, loginUrl);

Hope I made sense.

Regards,
Anindya

 
Sathish balajiSathish balaji
Yep, either pass null value or dont specify proxuURL parameter. As you see in below logic, if the proxy URL is null then it frames the proxy URL automatically. Code snippet taken from GITHUB. -- forcetk.js
forcetk.Client = function (clientId, loginUrl, proxyUrl) {
	        'use strict';
	        this.clientId = clientId;
	        this.loginUrl = loginUrl || 'https://login.salesforce.com/';
	        if (proxyUrl === undefined || proxyUrl === null) {
	            if (location.protocol === 'file:' || location.protocol === 'ms-appx:') {
	                // In PhoneGap
	                this.proxyUrl = null;
	            } else {
	                // In Visualforce - still need proxyUrl for Apex REST methods
	                this.proxyUrl = location.protocol + "//" + location.hostname
	                    + "/services/proxy";
	            }
	            this.authzHeader = "Authorization";
	        } else {
	            // On a server outside VF
	            this.proxyUrl = proxyUrl;
	            this.authzHeader = "X-Authorization";
	        }
Anindya ChattopadhyayAnindya Chattopadhyay
Thanks. It will simply be:

var client = new forcetk.Client(clientId, loginUrl,null);
Got it.

Thanks again!
Anindya
Sathish balajiSathish balaji
Thanks Anindya, Kindly mark it as best answer if it helps, so that others could refer it.
Anindya ChattopadhyayAnindya Chattopadhyay
Definitely!
Adam Robert 7Adam Robert 7
Thanks for your fabulous post! I enjoy while reading your post, you happen to be a great author. When I am free than reading your blog. I want to encourage yourself to continue to share fabulous posts, have a nice day! If you required best <a href="https://www.lojiqwpcare.com/wordpress-customer-service.html/"> WordPress Customer Service</a>, <a href="https://www.lojiqwpcare.com/wordpress-development.html/">WordPress Development Company</a> then contact us at any time.