Why is a fresh and active API token for the CRM “My Class” perceived as invalid during authentication?
I’m trying to connect to the CRM “My Class” API using the following Python code:
import requests
headers = {
"x-access-token": "<here's the active token from the website>"
}
response = requests.get("https://api.moyklass.com/v1/company/classes", headers=headers)
print(response.status_code)
print(response.text)
When executing the code, I get a 401 error with the message: {"code":"Unauthorized","message":"x-access-token is invalid"}. The token is fresh and active on the website, but the API doesn’t accept it. What could be the reason and how can I solve this problem?
Fresh and Active API Token for CRM “My Class” May Be Perceived as Invalid for Several Reasons
Despite appearing as valid in the system interface, a fresh and active API token for CRM “My Class” may be perceived as invalid for several reasons. The main issues are related to the token transmission format, its correctness, and compliance with API requirements.
Table of Contents
- Main Reasons for 401 Error
- Token Verification and Formatting
- Access Rights Settings
- Technical Aspects of Authentication
- Problem Solving
- Examples of Correct API Usage
Main Reasons for 401 Error
A 401 error with the message “x-access-token is invalid” usually occurs for the following reasons:
- Incorrect header format - the token may require a “Bearer” prefix
- Spaces or special characters - there may be hidden characters in the token
- Incorrect token type - possibly using a different token type than expected by the API
- Limited token rights - the token may not have access to the requested resource
- Caching issues - previous invalid requests may have affected validation
Important: Even if the token appears active in the CRM interface, this doesn’t guarantee it’s correct for API work.
Token Verification and Formatting
Before using the token, you need to ensure its correctness:
- Copy the token without spaces - check that no extra characters were added when copying
- Check token length - a typical API token should contain a sufficient number of characters
- Ensure no quotes - there should be no extra quotes around the token in the code
# Incorrect format
headers = {
"x-access-token": "\"<token>\"" # Extra quotes
}
# Correct format
headers = {
"x-access-token": "<token>" # Without extra characters
}
Access Rights Settings
The token must have appropriate rights to access the requested resource:
- Check token rights in the API settings of CRM “My Class”
- Ensure access to the
/v1/company/classesmethod - Check if the token is restricted by IP addresses or domains
If the token is IP-restricted, ensure requests are sent from an allowed IP address.
Technical Aspects of Authentication
Based on research of various CRM systems, common principles for working with tokens can be identified:
- X-Access-Token header - used in many CRM systems to transmit the token
- Request formatting - correctly specified Content-Type may be important
- Token validation - each system has its own validation rules
Many modern APIs require specifying content type even for GET-requests:
headers = {
"x-access-token": "<token>",
"Content-Type": "application/json"
}
Problem Solving
Step-by-step instructions for solving the problem:
1. Check token with a validator
Create a simple script to validate the token:
import requests
def validate_token(token):
headers = {
"x-access-token": token
}
try:
response = requests.get("https://api.moyklass.com/v1/company/info", headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
return response.status_code == 200
except Exception as e:
print(f"Error: {e}")
return False
# Example usage
token = "your_token_here"
is_valid = validate_token(token)
print(f"Token is valid: {is_valid}")
2. Try different token formats
# Option 1: Without prefix
headers = {"x-access-token": "<token>"}
# Option 2: With Bearer prefix
headers = {"x-access-token": "Bearer <token>"}
# Option 3: With Token prefix
headers = {"x-access-token": "Token <token>"}
3. Check documentation
Ensure you’re using the current API version. Sometimes developers change authentication methods in new versions.
4. Contact support
If nothing helps, contact technical support for CRM “My Class” with information:
- Error text
- API version
- Example code with token (without revealing the actual token)
Examples of Correct API Usage
Correct request example:
import requests
# Ensure the token is copied without extra characters
api_token = "your_real_token_here"
headers = {
"x-access-token": api_token,
"Content-Type": "application/json",
"User-Agent": "MyApp/1.0"
}
try:
response = requests.get(
"https://api.moyklass.com/v1/company/classes",
headers=headers,
timeout=30
)
if response.status_code == 200:
print("Successful request:")
print(response.json())
else:
print(f"Error {response.status_code}:")
print(response.text)
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
Error handling:
def make_api_request(token, endpoint):
headers = {
"x-access-token": token,
"Content-Type": "application/json"
}
try:
response = requests.get(f"https://api.moyklass.com{endpoint}", headers=headers)
if response.status_code == 401:
print("Authentication error. Check the token.")
elif response.status_code == 403:
print("Access denied. Check token permissions.")
elif response.status_code == 404:
print("Resource not found. Check the URL.")
else:
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
return None
Sources
- CRM ‘My Class’ API - api.moyklass.com
- REST API Alpha CRM - authentication examples
- Authorization through REST API
- REST API authentication methods
- API authentication | Unified Identity Management
Conclusion
Main reasons why a fresh API token for CRM “My Class” may be invalid:
- Incorrect transmission format - check for extra characters and spaces
- Lack of necessary rights - ensure the token has access to requested methods
- API changes - check documentation currency and authentication methods
- Technical limitations - the token may be IP-restricted or require additional headers
To solve the problem, it’s recommended to:
- Thoroughly check the token for hidden characters
- Experiment with different token transmission formats
- Contact technical support for CRM “My Class” to get current authentication information
- Use code examples from official documentation
Remember that even an active interface token may require special handling when working with the API.