- Update channel settings with HTTP PUT: https://www.mathworks.com/help/thingspeak/writesettings.html
- Error codes: https://www.mathworks.com/help/thingspeak/error-codes.html
Updating channel settings - field descriptions via python
11 views (last 30 days)
Show older comments
I'm trying to update my channel settings via a PUT request using Python and the Requests library. I'm following the example and guidance that's shown here: Mathworks-WriteSettings.
Following the example URL as a guide I get a URL parameter string that appears to meet the requirements, however I get the following error mesage: {"status":"401","error":{"error_code":"error_auth_required","message":"Authorization Required","details":"Please provide proper authentication details."}}
I have found a couple posts with similar issues and the fix action guidance was to ensure that the channel ID and API write keys are correct and match each other (i.e. the right write key for the right channel). I have double-checked mine a dozen times and I'm successfully sending actual data to the channel using the same key, so I'm pretty confident those parameters are correct.
In the documentation, the example URL is shown in two ways -- one way has ".json" appended to the primary URL (before the ? parameters). There's another example further down the page that shows the URL without .json appended.
If I remove the .json from my request, I do actually get a response that says I've successfully updated the channel, such as:
{'api_key': '<the_actual_api_key', 'field1': 'Air Temp', etc........}
Field names updated successfully!
However, no updates actually appear in the channel settings! As I understand it, the only purpose of the .json tag is to let ThingSpeak know whether to provide its response in json format as opposed to html (which appears to be the default response format).
I can post the entire python code if useful but it mostly it just creates the api_url shown below.
Any and all suggestions welcome -- thank you!!
Python code:
% # This passes authentication, returns 200 - but doesn't effect any actual settings changes...
api_url = 'https://api.thingspeak.com/channels/2475657?api_key=<actual_key>&field7=AirTemp'
% # This returns 401
api_url = 'https://api.thingspeak.com/channels/2475657.json?api_key=<actual_key>&field7=AirTemp'
0 Comments
Accepted Answer
Garmit Pant
on 19 Apr 2024
From what I gather, you are trying to communicate with ThingSpeak using REST API and are using the "requests" python library to use the REST API. You want to add modify the channel settings.
Error status “401” with error code “error_auth_required” signifies that the authentication details are incorrect, and the correct channel API key or user API key need to be provided. To update the channel settings using the “PUT” method, the URL needs to be of the form:
https://api.thingspeak.com/channels/<channel_id>.<format>
Thus, format should necessarily be specified as “json” or “xml” in the URL. Additionally, the body parameters of the request have the “api_key” parameter as a necessity. You need to specify the “User API key” and not the “Channel API key” for the “PUT” method. You can find the “User API key” in your profile.
You can use the following Python code snippet to use the “requests” library to use the “PUT” method.
%This is Python code. Remove the '%' from comments to run in Python.
import requests
%# Example URL and API key placeholders
url = "https://api.thingspeak.com/channels/<your_channel_id>.json"
api_key = "<your_user_api_key>"
%# Headers may need to include content type or authorization based on the API
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
%# Example data payload you want to send in the PUT request
data = {
"api_key": api_key,
"field7": <desired_value>,
%# Add other fields or configurations as needed
}
%# Making the PUT request
response = requests.put(url, json=data)
%# Checking the response
if response.status_code == 200:
print("Success:", response.json())
else:
print("Error:", response.status_code, response.text)
For further understanding, I suggest you refer to the following MathWorks Documentation:
Hope you find the above explanation and suggestions useful!
4 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!