Getting authentication failure when using POST from ESP32

조회 수: 8 (최근 30일)
avner gideobi
avner gideobi 2020년 3월 27일
댓글: Vinod 2020년 4월 7일
Trying to POST a CSV bulk update to update my channel.
Failing and getting
"401" as an http error code, and
"Please provide proper authentication details." as body.
Same thing does work with CURL.
What am I missing ????
thanks for the help.
#include <Arduino.h>
const char* ssid = "XXX";
const char* password = "XXX";
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
void setup() {
Serial.begin(115200);
initiateNetwork();
thingsSpeakLogger();
}
void loop() {}
int thingsSpeakLogger(){
WiFiClientSecure *client = new WiFiClientSecure;
HTTPClient https;
String payload = "write_api_key=XXXXXXXXXXX&time_format=relative&updates=4,1.1,2,0.3,,,6,7.7,0.8,41.2,19.5,100,ok|3,1,2,3,4,5,6,7,8,41.2,25.1,110,rising";
String connectionString = "https://api.thingspeak.com/channels/12345678/bulk_update.csv";
if (https.begin(*client, connectionString)) {
https.addHeader("Content-Type", "x-www-form-urlencoded");
Serial.println(https.POST(payload));
Serial.println(https.getString());
delete client;
}
}
void initiateNetwork() {
WiFi.begin(ssid, password); delay(1000);
while (WiFi.status() != WL_CONNECTED) {
delay(300); Serial.print(".");
}
}

답변 (3개)

Vinod
Vinod 2020년 3월 27일
  댓글 수: 1
Christopher Stapels
Christopher Stapels 2020년 3월 27일
The example Vinod shared should work well on ESP 32 as well.
Can you try the regular update in a browser windor with the same api key?
https://api.thingspeak.com/update.json?api_key=xxxxxxxxxxxxxxxx&field1=10
Also double check your channel ID. I have made that error myself before.

댓글을 달려면 로그인하십시오.


avner gideobi
avner gideobi 2020년 3월 27일
Thank you Vinod and Christopher for your quick reply!
for start, hitting the URL with the right api key works. Also from CURL with POST command.
I am building a big IoT project which the ThingsSpeak is a small part of it. In the project I use modern libraries such as WiFiClientSecure which greatly simplify communication, and eliminate the need to manually implement POST requests.
So, the problem is probably somewhere between the way I implemented it and Thingsspeak.
Would appriciate any help to attempt.
  댓글 수: 1
Vinod
Vinod 2020년 3월 27일
If you are implementing your own connectivity, I would recommend taking a look at this:
It has an implementation you can use as a starting point for your own.

댓글을 달려면 로그인하십시오.


avner gideobi
avner gideobi 2020년 3월 27일
Thanks again Vinod.
Good suggestion. I started there, unfortunately it dosen't support bulk CSV update, hence I need to do it myself.
  댓글 수: 1
Vinod
Vinod 2020년 4월 7일
I would look at the response code you are getting to try and troubleshoot the problem. A 401 response usually means that you have an incorrect API key, or it doesn't correspond to the channel you are updating. If you are able to get it working in CURL, then I would compare the CURL request to what is being sent from your Arduino as a starting point.

댓글을 달려면 로그인하십시오.

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 REST API에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by