Hello,
I tried posting some values from my ESP8266 to thingspeak but it failed with an 401 error.
To be sure that the error was not in my code I tred loading the example code "WriteMultipleFields" with no changes, created a new channel and renewed the API key mulitple times.
This is the error message:
Connected.
ts::setField (field: 1 value: "0")
ts::setField (field: 2 value: "3")
ts::setField (field: 3 value: "31")
ts::setField (field: 4 value: "88")
ts::setStatus(status: field1 is less than field2")
Connect to default ThingSpeak: api.thingspeak.com:80...Success.
ts::writeFields (channelNumber: 964373 writeAPIKey: XXXXXXXXXXXXXXXX
Got Status of 200
Content Length: 1
Found end of header
Response: "0"
Entry ID "0" (0)
disconnected.
Problem updating channel. HTTP error code -401
Any suggestions on what could be wrong?

 채택된 답변

Vinod
Vinod 2020년 1월 22일

0 개 추천

Are you still seeing a '0' response code to your requests to update a channel?

댓글 수: 1

Ctreb
Ctreb 2020년 1월 22일
Thanks, statuscode 2 now and updtes works as expected.

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

추가 답변 (5개)

Christopher Stapels
Christopher Stapels 2020년 1월 21일
편집: Christopher Stapels 2020년 1월 22일

0 개 추천

Are you able to update your channel from a web browser?
Use the format
https://api.thingspeak.com/update?api_key=xxxxxxxxxxxxxxxx$field1=432
On the Error Codes page you can see the 401 is authorization required. Since ThingSpeak has a lot of API keys, it is possible that you grabbed the wrong key. Be sure its the channel write api key from the api keys tabl of the channel view.
Let us know if that works. If not, we can look into it further.
Ctreb
Ctreb 2020년 1월 21일

0 개 추천

Hello,
Response code 0 and no update.
David Bird
David Bird 2020년 3월 23일
편집: David Bird 2020년 3월 23일

0 개 추천

Hello,
I'm also getting 401 authriosation errors when using the Bulk update API:
The delta-time version works OK:
{
"write_api_key": "WRITE_API_KEY",
"updates": [{
"delta_t": 4,
"field1": 1.0,
"field2": "2.0"
},
{
"delta_t": 2,
"field1": 1.1,
"field2": 2.2,
"elevation": 6,
"status": "Good"
}
]
}
But the absolute time version does not:
{
"write_api_key": "WRITE_API_KEY",
"updates": [{
"created_at": "2020-03-23 09:26:02 -0000",
"field1": "1.0",
"field2": "2.0"
},
{
"created_at": "2020-03-23 10:27:27 -0000",
"field1": "1.1",
"field2": "2.2",
}
]
}
The JSON response from the server is:
{"status":"401","error":{"error_code":"error_auth_required","message":"Authorization Required","details":"Please provide proper authentication details."}}
So in the delta time version the Channel ID and Write key are correct and authorise, but in the absolute time variant they fail - it seems. I'm using
I note others have seen the same problem. It looks like there is an API problem.

댓글 수: 1

Christopher Stapels
Christopher Stapels 2020년 3월 23일
You have an extra comma in the second example:
"field2": "2.2",
should be
"field2": "2.2"
When I send it without to comma, it works. (with my write key and channel ID).
When I send it with the comma, I get a 400 error, not a 401. Can you check again and report if you are still seeing that error? We definitely want to be sure the correct error is returned.

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

David Bird
David Bird 2020년 3월 23일

0 개 추천

Actually that was an example from the API and I deleted a field, thereby leaving that stray ',', sorry about that. Heres a copy of my upload:
{
"write_api_key": "WRITE_API_KEY",
"updates": [{
"created_at": "2020-03-23 19:01:26 -0000",
"field1": 20.0,
"field2": 51.7,
"field3": 1026
},
{
"created_at": "2020-03-23 19:02:28 -0000",
"field1": 20.1
"field2": 51.7
"field3": 1026
}
]
}
Which returns 401 authroisation required.

댓글 수: 7

David Bird
David Bird 2020년 3월 23일
the channel numbner is 87123
Christopher Stapels
Christopher Stapels 2020년 3월 23일
Are you able to update the channel for a single field using the same api key?
For example
I tried both of the examples you provided and they worked for me on my channels. The only reason I can think of theat you would get a 401 is if the API key was incorrect. Some
Are you using POSTMAN? Are you setting the content type header?
David Bird
David Bird 2020년 3월 23일
I’m using an ESP32 and POST, what I’ve established is when using the delta_t API call which is identical except for ‘created_by’ so the calling mechanism works as do the channel ID and write_api_key it’s just the JSON content that’s different. So a delta_t JSON works perfectly but as others have reported the absolute time JSON does not. I’ve tried both http and https calls and they both give identical results. The JSON I copied in earlier is verbatim and it meets the API exactly. I can’t see any other error information to see what the problem might be, I reported the full response in the initial posting here.
David Bird
David Bird 2020년 3월 23일
편집: David Bird 2020년 3월 23일
BTW is the iso8601 time format correct? Your API example does not correspond exactly to iso8691 format
David Bird
David Bird 2020년 3월 23일
BTW I tried this and it works:
You can see the entry on channel ID 87123
David Bird
David Bird 2020년 3월 24일
This is my POST request code:
if (client.connect(server, 80)) { // POST data to ThingSpeak
client.println("POST /channels/" + String(THINGSPEAK_CH_ID) + "/bulk_update.json HTTP/1.1"); // Replace YOUR-CHANNEL-ID with your ThingSpeak channel ID
client.println("Host: api.thingspeak.com");
client.println("User-Agent: mw.doc.bulk-update (Arduino ESP8266)");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.println("Content-Length: " + String(jsonBuffer.length()));
client.println();
client.println(jsonBuffer);
}
Christopher Stapels
Christopher Stapels 2021년 4월 19일
편집: Christopher Stapels 2021년 4월 20일
@David BirdYou seem to be missing the API key in the latest version just above.

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

Cosimo Carbonelli
Cosimo Carbonelli 2020년 7월 25일

0 개 추천

Sorry I have the same problem.
The content that i send through a POST to https://api.thingspeak.com/channels/MYCHANNELID/bulk_update.json is
{
"write_api_key": "MYAPIKEY",
"updates": [{
"created_at": "2020-07-24 11:34:28 -0738",
"field2": -68,
"field3": 25.4,
"field4": 1005,
"field5": 118,
"field6": 0,
"field7": 0
}]
}
and the error that I get is
{
"status": "401",
"error": {
"error_code": "error_auth_required",
"message": "Authorization Required",
"details": "Please provide proper authentication details."
}
}
but if I made GET requests using
it's all right.
What's wrong?
many thanks
Cosimo

댓글 수: 2

Cosimo Carbonelli
Cosimo Carbonelli 2020년 7월 26일
Hi,
I found by myself the answer.
The problem was in the way I have built the json variable (in my Node RED environment). So it was a JavaScript problem and not a ThingSpeak API problem.
sorry
Cosimo
cog
cog 2023년 2월 12일
Cosimo, what was it about how you were building the JSON that was causing the error?

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

커뮤니티

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

카테고리

도움말 센터File Exchange에서 ThingSpeak에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 1월 18일

댓글:

cog
2023년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by