Here are 20 lines of Python code that do approximately the same as the React Native code referred to by @renato (except no compatibiliy with Discourse 2.5 - I don’t need that)
It works well, assuming that you are using basic username-password based login. I will still look into the alternative methods, using the Discourse SSO login as configured in the Discourse instance.
import requests
import json
def discourse_authenticate(url, name, password):
session = requests.Session()
session.headers.update({'X-Requested-With': 'XMLHttpRequest'})
r1 = session.get(url + '/session/csrf')
csrf_token = json.loads(r1.text).get('csrf')
r2 = session.post(url + '/session',
data={
'login': name,
'password': password,
'authenticity_token': csrf_token,
},
)
if r2.status_code != 200:
return None
return json.loads(r2.text)