Skip to content

Commit df0d071

Browse files
authored
Merge pull request #58 from arduino/fix-429-errors
fix: add the organization header to the token request for http nodes
2 parents 9ae46e0 + c424f9d commit df0d071

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

arduino-iot-cloud.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = function (RED) {
7575
try {
7676

7777
if (config.thing !== "" && config.property !== "") {
78-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
78+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
7979
if (this.arduinoRestClient){
8080
this.arduinoRestClient.openConnections++;
8181
this.organization = config.organization;
@@ -152,7 +152,7 @@ module.exports = function (RED) {
152152
this.timeWindowUnit = config.timeWindowUnit;
153153
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
154154
try {
155-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
155+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
156156
if (this.arduinoRestClient){
157157
this.arduinoRestClient.openConnections++;
158158
if (config.thing !== "" && config.property !== "") {
@@ -251,7 +251,7 @@ module.exports = function (RED) {
251251
this.organization = config.organization;
252252
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
253253
try {
254-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
254+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
255255
if (this.arduinoRestClient){
256256
this.arduinoRestClient.openConnections++;
257257
if (config.thing !== "" && config.property !== "") {
@@ -340,7 +340,7 @@ module.exports = function (RED) {
340340
try {
341341

342342
if (config.thing !== "" && config.property !== "") {
343-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
343+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
344344
if (this.arduinoRestClient){
345345
this.arduinoRestClient.openConnections++;
346346
this.organization = config.organization;
@@ -432,15 +432,15 @@ module.exports = function (RED) {
432432
clientid: req.query.clientid,
433433
clientsecret: req.query.clientsecret
434434
}
435-
});
435+
}, this.organization);
436436
} else if (req.query.connectionid) {
437437
const connectionConfig = RED.nodes.getNode(req.query.connectionid);
438438
if (!connectionConfig) {
439439
str=RED._("arduino-iot-cloud.connection-error.no-cred-available");
440440
console.log(str);
441441
return res.send(JSON.stringify({ error: str }));
442442
}
443-
arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
443+
arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
444444
} else {
445445
str=RED._("arduino-iot-cloud.connection-error.no-cred-available");
446446
console.log(str);

utils/arduino-connection-manager.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const getClientMutex = new Mutex();
4040
var numRetry=0;
4141

4242

43-
async function getToken(connectionConfig) {
43+
async function getToken(connectionConfig, organizationID) {
4444
const dataToSend = {
4545
grant_type: 'client_credentials',
4646
client_id: connectionConfig.credentials.clientid,
@@ -49,12 +49,16 @@ async function getToken(connectionConfig) {
4949
};
5050

5151
try {
52+
var req = superagentsuperagent
53+
.post(accessTokenUri)
54+
.set('content-type', 'application/x-www-form-urlencoded')
55+
.set('accept', 'json')
5256

53-
var res = await superagent
54-
.post(accessTokenUri)
55-
.set('content-type', 'application/x-www-form-urlencoded')
56-
.set('accept', 'json')
57-
.send(dataToSend);
57+
if (organizationID) {
58+
req.set('X-Organization', organizationID)
59+
}
60+
61+
var res = await req.send(dataToSend);
5862
var token = res.body.access_token;
5963
var expires_in = res.body.expires_in * 0.8; // needed to change the token before it expires
6064
if (token !== undefined) {
@@ -161,7 +165,7 @@ async function getClientMqtt(connectionConfig, RED) {
161165

162166
}
163167

164-
async function getClientHttp(connectionConfig) {
168+
async function getClientHttp(connectionConfig, organizationID) {
165169

166170
if (!connectionConfig || !connectionConfig.credentials) {
167171
throw new Error("Cannot find cooonection config or credentials.");
@@ -172,7 +176,7 @@ async function getClientHttp(connectionConfig) {
172176
var clientHttp;
173177
if (user === -1) {
174178

175-
var tokenInfo = await getToken(connectionConfig);
179+
var tokenInfo = await getToken(connectionConfig, organizationID);
176180
if (tokenInfo !== undefined) {
177181
clientHttp = new ArduinoClientHttp.ArduinoClientHttp(tokenInfo.token);
178182

0 commit comments

Comments
 (0)