Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X
Hi,
I am able to send my Sensor data to Thingworx using REST calls from ESP8266 ,
but not able to fetch a property value from Thingworx using GET request instead getting 401 error(Unauthorised)
even though there is correct appKey. I can fetch data using Python but in "C " am getting problem.
Please help me where i went wrong.
Thank you!
Here is the code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include<arduino.h>
#include <ESP8266HTTPClient.h>
#define ACCEPT_TYPE "application/json"
const char* ssid = "D-Link_DIR-615";
const char* password = "cadoptiot@123";
const char TWPlatformBaseURL[] = "http://xxx.ngrok.io:80";
const char appKey[] = "axxxxxxxxxxxxxxxxx8";
WiFiClient client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());;
}
void loop()
{
HTTPClient http;
int httpCode = -1;
String response = "";
Serial.print("[httpGetPropertry] begin...");
String fullRequestURL = String(TWPlatformBaseURL) + "/Thingworx/Things/Capacity/Properties/hour?appKey=" + String(appKey);
http.begin(fullRequestURL);
http.addHeader("Accept",ACCEPT_TYPE,false,false);
Serial.println("GET URL>" + fullRequestURL +"<");
// start connection and send HTTP header
httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0)
{
response = http.getString();
Serial.printf("[httpGetPropertry] response code:%d body>",httpCode);
Serial.println(response + "<\n");
} else
{
Serial.printf("[httpGetPropertry] failed, error: %s\n\n", http.errorToString(httpCode).c_str());
}
http.end();
}
Solved! Go to Solution.
Hello,
Check the expiration time of your appKey(a lot of times I got this error because expiration time, and maybe when you did in python, it was still valid), and check the user permissions of that appKey, if he has the permissions to do what you are trying to do with this code.
Cheers,
Vinicius.
The Serial Monitor shows:
[httpGetPropertry] begin...GET URL>http://baz.ngrok.io:80/Thingworx/Things/Capacity/Properties/hour?appKey=a3188ba0-944a-486c-92a1-d434c6154898<
[httpGetPropertry] response code:401 body><
Hello,
Check the expiration time of your appKey(a lot of times I got this error because expiration time, and maybe when you did in python, it was still valid), and check the user permissions of that appKey, if he has the permissions to do what you are trying to do with this code.
Cheers,
Vinicius.
Hi,
I have checked it properly , i have used same appkey for both Python and Arduino code
and i am administrator, i had all permissions i guess..
Thankyou!
Akshay
Yeah Thank you, i have checked in Platform Subsytem and allowed permission for
"Allow Application Key as URL Parameter"
|
Now its working fine.
Regards,
Akshay