MQTT connection result code 5

I have data from a temperature and humidity sensor uploading to the things network. I would like to get access to the data to store it in a database. To do this I am attempting to use Mqtt. I have tried several sources of code I have found in forum discussions. The following is an example. I keep getting a connection result code 5. My understanding is that the connection is not authorised. I have tried a number of different things but have been unable to get connected.

Any insights as to why the connection is not valid would be helpful.

Thanks

John

A Python program to listen to your devices with MQTT

Get data from MQTT server

Run this with python 3, install paho.mqtt prior to use

import paho.mqtt.client as mqtt
import json
import base64

APPID = “dth22first”
PSW = ‘ttn-account-v2.w5A_vMporch redacted’

#Call back functions

gives connection message

def on_connect(mqttc, mosq, obj,rc):
print(“Connected with result code:”+str(rc))
# subscribe for all devices of user
mqttc.subscribe(’+/devices/+/up’)

gives message from device

def on_message(mqttc,obj,msg):
try:
#print(msg.payload)
x = json.loads(msg.payload.decode(‘utf-8’))
device = x[“dev_id”]
counter = x[“counter”]
payload_raw = x[“payload_raw”]
payload_fields = x[“payload_fields”]
datetime = x[“metadata”][“time”]
gateways = x[“metadata”][“gateways”]
# print for every gateway that has received the message and extract RSSI
for gw in gateways:
gateway_id = gw[“gtw_id”]
rssi = gw[“rssi”]
print(datetime + ", " + device + ", " + str(counter) + ", "+ gateway_id + ", "+ str(rssi) + ", " + str(payload_fields))
except Exception as e:
print(e)
pass

def on_publish(mosq, obj, mid):
print("mid: " + str(mid))

def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))

def on_log(mqttc,obj,level,buf):
print(“message:” + str(buf))
print(“userdata:” + str(obj))

mqttc= mqtt.Client()

Assign event callbacks

mqttc.on_connect=on_connect
mqttc.on_message=on_message

mqttc.username_pw_set(APPID, PSW)
mqttc.connect(“thethings.meshed.com.au”,1883,60)

and listen to server

run = True
while run:
mqttc.loop()