Tiles/WMS sources aregive you raster data. YouSo, you have to use QgsRasterLayer()
instead of QgsVectorLayer()
.
Try in this way:
...
rlayer = QgsRasterLayer(urlWithParams, 'OpenStreetMap', 'wms') # EDIT THIS LINE
if rlayer.isValid():
QgsProject.instance().addMapLayer(rlayer)
else:
print('invalid layer')