Forum Discussion
"Invalid JWT Error When Sending Messages from Azure Bot to Skype User
I'm encountering an "Invalid JWT" error when trying to send a non-reply message from an Azure Bot to a Skype user, despite using what appears to be a valid token. Here's a breakdown of my setup:
- I successfully generate an access token using OAuth client credentials for the Microsoft Bot Framework.
- I create a conversation ID successfully, but when I attempt to send a message using this ID, I receive a 401 error with "Invalid JWT."
Here is the relevant part of my code:
import requests
# Setup for token generation
service_url = "https://smba.trafficmanager.net/apis"
token_url = f'https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token'
token_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
token_payload = {
'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_password,
'scope': 'https://api.botframework.com/.default'
}
# Token request
token_response = requests.post(token_url, headers=token_headers, data=token_payload)
token = token_response.json()['access_token']
# Setup for creating a conversation
conversation_headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
conversation_url = f"{service_url}/v3/conversations"
conversation_payload = {
"bot": {"id": f"28:{app_id}", "name": "botname"},
"isGroup": False,
"members": [{"id": skype_id, "name": "Milkiyas Gebru"}],
"topicName": "New Conversation"
}
conversation_response = requests.post(conversation_url, headers=conversation_headers, json=conversation_payload)
conversation_id = conversation_response.json()["id"]
# Setup for sending a message
message_url = f"{service_url}/v3/conversations/{conversation_id}/activities"
message_headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
message_payload = {"type": "message", "text": "My bots reply"}
message_response = requests.post(message_url, headers=message_headers, json=message_payload)
print("Create Message Response: ", message_response.json(), message_response.status_code)
The response I get indicates an authorization error:
Create Message Response: {'error': {'code': 'AuthorizationError', 'message': 'Invalid JWT.'}} 401
Has anyone experienced this issue before, or does anyone know what might be causing the JWT to be considered invalid? Any insights or suggestions would be greatly appreciated!