Clear Filters
Clear Filters

Updating channel settings - field descriptions via python

11 views (last 30 days)
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'

Accepted Answer

Garmit Pant
Garmit Pant on 19 Apr 2024
Hello Norm O'Foran
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_requiredsignifies 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:
  1. Update channel settings with HTTP PUT: https://www.mathworks.com/help/thingspeak/writesettings.html
  2. Error codes: https://www.mathworks.com/help/thingspeak/error-codes.html
Hope you find the above explanation and suggestions useful!
  4 Comments
Norm O'Foran
Norm O'Foran on 22 Apr 2024
@Garmit Pant Success!!!!!! It works.
That was literally the key! (pun totally intended).
I didn't realize the distinction and had forgotten there even was another key. 90% of the time it is something obvious, but somehow not.
And looking again at the documentation I see:
api_key(Required) Specify User API Key, which you can find in your profile. This key is different from the channel API keys.
Thanks so much!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!