Skip to main content
1-Visitor
8月 6, 2020 7:59 午前 UTC
解決済み

ThingWorx Platform - Connection to Saleforce Endpoint with OAuth 2.0

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!

 

 

otluk回答
1-Visitor
8月 12, 2020 4:30 午後 UTC

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));
}

3 の返信

16-Pearl
8月 6, 2020 8:20 午前 UTC

I do not have an example to share but probably ThingWorx Flow is what you are looking for. 

Configuring & loading OAuth in ThingWorx Flow

翻訳
otluk1-Visitor作成者
1-Visitor
8月 6, 2020 8:23 午前 UTC

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)

翻訳
otluk1-Visitor作成者回答
1-Visitor
8月 12, 2020 4:30 午後 UTC

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));
}
翻訳