JSON Bulk Write to ThingSpeak Channel, 401 Unauthorized Error

I am attempting to bulk write some JSON data to a ThingSpeak channel using the API. An example payload is:
% Payload is
data =
{
'write_api_key': <my_api_write_key>,
'updates': [
{'field5': 833.205, 'field4': 6, 'field3': 775.648, 'field2': 24.2523, 'delta_t': 1, 'field1': 59.1391},
{'field5': 833.199, 'field4': 6, 'field3': 771.03, 'field2': 24.2844, 'delta_t': 2, 'field1': 59.1331},
{'field5': 833.208, 'field4': 6, 'field3': 750.343, 'field2': 24.3008, 'delta_t': 3, 'field1': 59.1405},
{'field5': 833.208, 'field4': 7, 'field3': None, 'field2': 24.2903, 'delta_t': 5, 'field1': 59.1479},
{'field5': 833.216, 'field4': 7, 'field3': 748.45, 'field2': 24.2965, 'delta_t': 6, 'field1': 59.1686},
{'field5': 833.21, 'field4': 8, 'field3': None, 'field2': 24.2914, 'delta_t': 7, 'field1': 59.1756},
{'field5': 833.204, 'field4': 8, 'field3': 738.847, 'field2': 24.2867, 'delta_t': 8, 'field1': 59.1943},
{'field5': 833.208, 'field4': 8, 'field3': 732.472, 'field2': 24.2814, 'delta_t': 10, 'field1': 59.1964},
{'field5': 833.212, 'field4': 8, 'field3': None, 'field2': 24.2599, 'delta_t': 11, 'field1': 59.1971},
{'field5': 833.207, 'field4': 8, 'field3': 737.475, 'field2': 24.2658, 'delta_t': 12, 'field1': 59.2122},
{'field5': 833.212, 'field4': 8, 'field3': None, 'field2': 24.2498, 'delta_t': 14, 'field1': 59.2161},
{'field5': 833.209, 'field4': 7, 'field3': 749.432, 'field2': 24.2503, 'delta_t': 15, 'field1': 59.2298},
{'field5': 833.208, 'field4': 7, 'field3': 760.224, 'field2': 24.2396, 'delta_t': 16, 'field1': 59.2308}
]
}
I am sending this payload programatically, in Python, with the requests library using the following command:
response = requests.post(url='https://api.thingspeak.com/channels/<my_channel_id>/bulk_update.json', ...
data=json.dumps(data))
This results in the response below:
% Response
{
"status": "401",
"error": {
"error_code":"error_auth_required",
"message":"Authorization Required",
"details":"Please provide proper authentication details."
}
}
I am certain that the API write key and channel_id are correct. I have used both for single writes successfully. I am using a free ThingSpeak account. I have also tried adding headers to the requests.post() call to indicate explicitly that this is json data. I'm not sure why this is occuring and would sincerely appreciate any pointers.

답변 (1개)

Christopher Stapels
Christopher Stapels 2023년 2월 13일

0 개 추천

It seems that ThingSpeak is not understanding your API key. This example for an RPI uses python. Does your code look similar?
Also you may want to put delta t first, the endpoint may be expecting the time data first. See the example Bulk Write with Reletive Timestamps in the bulk jso write doc.

댓글 수: 3

I have exactly the same problem as the OP but only with a single field in a bulk update. I am doing exactly what is shown in the documentation.
{
"write_api_key":my api key,
"updates":[
{"created_at":"2023-08-02 15:50:20","field1":1000},
{"created_at":"2023-08-02 15:50:20","field1":1500}
]
}
I don't know what I am doing wrong
Are you able to post to the regular write endpoint with the same API key and channel ID?
@Erol Probably better to start a new post though. Make sure you show the whole part of the code that does the posting, and desribe your hardware please.

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

커뮤니티

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

카테고리

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

제품

질문:

cog
2023년 2월 12일

편집:

2023년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by