Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Hello PTC Developer Community,
I am looking for Best Practices / Examples how to connect my thing to external platforms like Salesforce where I need to use OAuth 2.0 Client in my Service.
Steps I like to perform:
1) OAuth 2.0 with the platform (Here I am looking for examples and best practices)
2) Post JSON with current Sensor Values from my Thing (e.g. could be AnyDataChange or via Timer) (This is straight foward with Contentloader Function and the Guide)
see also here: https://community.ptc.com/t5/ThingWorx-Developers/OAuth-2-0-using-Thingworx-Content-Loader-Services/m-p/681073#M47082 @praskumar seems to have a similar question.
I started with the Contentloader Function Guide: https://developer.thingworx.com/resources/guides/xml-json-in-thingworx
Where I am not sure how I can setup my OAuth 2.0 parameter described here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
Any hints how to setup OAuth 2.0 ?
Anyone got some hints how to connect your thing to external services like Salesforce in Thingworx Foundation 8.5?
Please consider that in my case Thingworx Flow is currently not an option.
Thank you!
Solved! Go to Solution.
Solution:
// credentials as x-www-form-urlencoded string
var auth_payload = "grant_type=password&client_id=";
var auth_params = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding":"gzip, deflate, br",
"Accept": "*/*"
} /* JSON */,
url: "https://test.salesforce.com/services/oauth2/token" /* STRING */,
content: auth_payload
};
var auth_response = JSON.parse(Resources["ContentLoaderFunctions"].PostText(auth_params).split("^")[0]);
var token = auth_response["access_token"];
var token_type = auth_response["token_type"];
var request_url = auth_response["instance_url"];
//Send data to Salesforce
var request_payload = {
"FL_Temperature__c": Things["Thing_01"].temperature,
"FL_Timestamp__c": null
};
var request_params = {
headers: {
"Authorization": token_type + " " + token,
"Content-Type": "application/json",
"Accept-Encoding":"gzip, deflate, br",
"Accept": "application/json"
} /* JSON */,
url: request_url + "/services/data/v48.0/sobjects/FL_Compressor__e",
content: request_payload /* JSON */
};
// result: JSON
var result = Resources["ContentLoaderFunctions"].PostJSON(request_params);
if (result.hasOwnProperty("success") && result.success) {
logger.info("DATA TRANSFERRED TO SF: " + JSON.stringify(result));
} else {
logger.warn("SF TRANSFER FAILED: " + JSON.stringify(result));
}
I do not have an example to share but probably ThingWorx Flow is what you are looking for.
Sorry I mentioned that ThingWorx Flow is not an option for - of course wiht ThingWorx Flow it is straight foward because it a NodeJS Server under neath -> I am looking for a integration in ThingWorx Platform as an alternative because I do not have a Flow installation available here.
My Question is related to this question: https://community.ptc.com/t5/ThingWorx-Developers/OAuth-2-0-using-Thingworx-Content-Loader-Services/m-p/681073#M47082 here basically the same question I have (but we posted at the same time)
Solution:
// credentials as x-www-form-urlencoded string
var auth_payload = "grant_type=password&client_id=";
var auth_params = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding":"gzip, deflate, br",
"Accept": "*/*"
} /* JSON */,
url: "https://test.salesforce.com/services/oauth2/token" /* STRING */,
content: auth_payload
};
var auth_response = JSON.parse(Resources["ContentLoaderFunctions"].PostText(auth_params).split("^")[0]);
var token = auth_response["access_token"];
var token_type = auth_response["token_type"];
var request_url = auth_response["instance_url"];
//Send data to Salesforce
var request_payload = {
"FL_Temperature__c": Things["Thing_01"].temperature,
"FL_Timestamp__c": null
};
var request_params = {
headers: {
"Authorization": token_type + " " + token,
"Content-Type": "application/json",
"Accept-Encoding":"gzip, deflate, br",
"Accept": "application/json"
} /* JSON */,
url: request_url + "/services/data/v48.0/sobjects/FL_Compressor__e",
content: request_payload /* JSON */
};
// result: JSON
var result = Resources["ContentLoaderFunctions"].PostJSON(request_params);
if (result.hasOwnProperty("success") && result.success) {
logger.info("DATA TRANSFERRED TO SF: " + JSON.stringify(result));
} else {
logger.warn("SF TRANSFER FAILED: " + JSON.stringify(result));
}