How to authenticate by OAUTH2 with external software?

How to authenticate by OAUTH2 with external software?Category: AnyHow to authenticate by OAUTH2 with external software?
Dominik Amann Staff asked 9 months ago

Hello there,

at the moment i am trying to authenticate at our virtual FORCAM factory with an external software based on Node.JS (Node-RED), but i only get error code 401.

Here are the credentials which i use:

msg.oauth2Request = {

“access_token_url”: “https://fcfhws.forcam.university/ffauth/”,
“credentials”: {
“grant_type”: “client_credentials”,
“client_id”: “MYIDHERE”,
“client_secret”: “MYSECRETHERE”,
“scope”: “read write” //openid
}
};

The client “EduAPI” was created under Webservices-API-Clients in the Workbench.

Best regards,
B. Engelmann

2 Answers
Dominik Amann Staff answered 9 months ago

Hello,

please try a simple HTTP GET

https://fcfhws.forcam.university/ffauth/oauth2.0/accessToken?client_id=MYIDHERE&client_secret=MYSECRETHERE&grant_type=client_credentials&scope=read%20write

This function returns a valid Bearer token. With this token you are able to access the BridgeAPI interfaces.

All the best,

Dominik

Dominik Amann Staff answered 9 months ago

With the answer above, here is the solution to get an access token:
Use http request node in Node-RED and perform a “GET” with the following “URL”:
https://fcfhws.forcam.university/ffauth/oauth2.0/accessToken?client_id=YOUR_ID_HERE&client_secret=YOUR_SECRET_HERE&grant_type=client_credentials&scope=read%20write
The user must be created with client_id and client_secret in the workbench under configuration->webservices->API clients
With the access token you can then use the FORCE Bridge API. It must be put in the HTTP header. e.g. with a function node:
var custom = “Bearer “;
custom += msg.payload.access_token;
msg.payload ={};
msg.responseUrl={};
msg.responseCookies={};
msg.redirectList={};
// in the URL put the API call, e.g. get all workplaces
msg.url = “https://YOUR_DOMAIN_HERE/ffwebservices/api/v3/workplaces”;
msg.headers = {
“Authorization” : custom,
};
return msg;