cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

External api access

karthik2
7-Bedrock

External api access

Hi friends,

how to access the client id,resource and client secret .I tried the below all formats but they are not working.please help me.This is for oauth2 access .

15 REPLIES 15
PaiChung
22-Sapphire I
(To:karthik2)

Is there actual API documentation?

Looks like what you attached are attempts, but what does the documentation say about the api?

Hi,

i want to access only those secret id and client resource only in my api.By using javascript.

Thanks,

karthik

PaiChung
22-Sapphire I
(To:karthik2)

Do you have an example of this call working and its return in for example Postman?

Hi Paichung,

Based on  client secret,resource and client id details in my code i want to get the access token.After that i passed my access token is input for the second api.please suggest me how to implement that code .I tried it in multiple ways this is not working.

.please help me.

Thanks,

karthik

PaiChung
22-Sapphire I
(To:karthik2)

I'm so sorry but I don't see any information about the APIs you are actually calling and how they are defined.

All I see is your code to call the APIs.

Were you to use something like Postman, are you able to make successful calls? What do the postman calls look like?

yes i am having 4 apis like this .But in my case what is the output i got in first api i passed this access token input to the second api.

Hello @karthik2,

 

If you're trying to use OAuth2 Client Credentials Flow (which I assumed based on your parameter names), then you're missing few things -- grant_type, content type, etc. Also, I'm really not sure that you can pass values like client_id as headers. Normally they are sent as application/x-www-form-urlencoded.

 

More details and examples here: https://auth0.com/docs/flows/guides/client-credentials/call-api-client-credentials

 

/ Constantine

rweido
7-Bedrock
(To:karthik2)

I've had success retrieving OAuth tokens as follows:

 

//build auth string
var auth = myClientID + ":" + myClientSecret;
//base64 encode it
var authentication = base64EncodeString(auth);
//set the content for the request
var content = "grant_type=client_credentials&scope=any";

//execute the request
var params = {
	headers: {"Authorization":"Basic " + authentication,"Content-Type":"application/x-www-form-urlencoded","Accept":"application/json"} /* JSON */,
	ignoreSSLErrors: true /* BOOLEAN */,
	url: "https://someurl.com/token" /* STRING */,
	content: content /* JSON */,
	timeout: 300 /* NUMBER */
};

//use post text to post the content, this will return a string
var responseJson = Resources["ContentLoaderFunctions"].PostText(params);
//the headers are appended at the end with a ^ character, split those off
var split = responseJson.split("^");
//convert the string of the response to a JSON
var json = JSON.parse(split[0]);
//get the access_token from the JSON
var result = json["access_token"];

 

Then, you can make your next API call with the token. 

karthik2
7-Bedrock
(To:rweido)

Hi rweido,

I am having below api 

https://corpqa.com/oauth/token i want to pass this api in thingworx i want output like below format

Token:sjklgfdlkdjfhgkjdhfgkdhfkjhgkhdkhfgldkjghldfkghdklfglkdfgjdlkfgjlkdglkdldlgdhgkjdfgldfk

access token: bearer

expires in:3600 actually i got the above output in postman successfully.but i am unable to got the output i.e;access token in thingworx.please help me.

Thanks and regards,

karthik

karthik2
7-Bedrock
(To:rweido)

Hi,

 i am unable to get access token for exected the below code.anybody is there to help me for this.

var params = {
ignoreSSLErrors: true /* BOOLEAN */,
url: "https://thingworxadfsnodeapp.apps.pd01e.edc1./adfs" /* STRING */,
};
var result = Resources["ContentLoaderFunctions"].PostJSON(params);

I got the ouput ike below:

Error executing service api1. Message :: thingworxadfsnodeapp.apps.pd01e.edc1./adfs.com - See Script Error Log for more details.

In postman this url is working properly.In chrome also it is working properly.But in case of thingworx i am unable to get the output like below format

{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6InI1VFl6SHFoX3BPZjVWYXRCdUI2eFF5cGFWUSJ9.eyJhdWQiOiJ1cm46dGNzdnM6dGNzZXJ2aWNlc2kxOnFhIiwiaXNzIjoiaHR0cHM6Ly9jb3JwcWEuc3RzLmZvcmQuY29tL2FkZnMvc2VydmljZXMvdHJ1c3QiLCJpYXQiOjE1OTIyNzYyNzAsImV4cCI6MTU5MjI3OTg3MCwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNzk2OTRkODgtZmQ2My00YTFjLWE3YmMtZTU3MzYwMmI3NWUwIiwiYXV0aG1ldGhvZCI6Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9hdXRoZW50aWNhdGlvbm1ldGhvZC9wYXNzd29yZCIsImF1dGhfdGltZSI6IjIwMjAtMDYtMTZUMDM6MDI6NTAuOTIzWiIsInZlciI6IjEuMCJ9.ExnBIziHji_r2eTJ5qEQPJAKD15gXT5qUsOK12e4IsNomwrrgaumKvE_tH3r_Iox65bGzk-ewafFt26xZZtVNZA3uUE-CyXU0q_deK79BRKnVWaL4OXAarvMCRqtuL4N3lFxtNjzbpB4z_WnDFWe4uM_gtvlAQW6Q4zBI7mQbge9-X1xTpQSlGA7z4uZKBwdGxZdlDMBWH3hji1X1P_XxlBTo1gNTn7Ap2BXUt4Re_LP2hHoALZfovSjbgzzY9PwFZsd0BBp6J5YmmrNexJd

Thanks,

karthik

CharlesJi
14-Alexandrite
(To:karthik2)

Hi @karthik2, I think the Resources["ContentLoaderFunctions"].PostJSON will do the trick.

However, the error details seem to be printed in Script Error Log, so could you please provide the error details?

BTW, you mentioned that you successfully obtained the results by using POSTMAN, could you also provide the details? (e.g., screenshots will be fine)

Hi paichung,

I already tried in all ways. I want to explain one more time i am having api like below

https://corpqa.sts.com/oauth2/token

after executing this api i get the ouput like

access token:"ajkhdnjkhdsmgflkjuhsrfdgn/fdlkjhpg8yusoio790lfdgbmkljoifkvlbm,klgdjbom"

token type:'bearer'

expires in:'3600'

whatever the access token i got like above i need to pass that api input for the second api.

here my problem is i want to write the code for aboive api in thingworx.please help me for this.this api is working in postman tool.i am not getting the access token in thingworx.

 

PaiChung
22-Sapphire I
(To:karthik2)

So you are having issues with even getting the initial token correct?

(if that part is working correctly you can just follow Rob's example)

 

Can you show us the Postman call?

Easiest is to export it, but edit the json to obscure the secret.

 

Also Rob's example shows a treatment of the response as string, If a full JSON is returned you can also treat that return as a JSON object.

Hi paichung,

here i want to write the code for acceessing the token by using url .so what is the code for that access token.

Thanks,

karthik

PaiChung
22-Sapphire I
(To:karthik2)

Which access token are you referring to? The token from the site you want to access afterwards with the token?

Did you look at Rob's example?

Top Tags