Search code examples
pythonurlfetchdiscourse

How to get current user with Discourse API


I'm accessing the Discourse API from python using urlfetch. The Get a single user by username endpoint requires a GET request such as /users/{username}.json

From a browser, this command returns a json response as expected, however from an API call like:

from google.appengine.api import urlfetch
result = urlfetch.fetch('{}/users/{}.json'.format(domain, username))

it returns a HTML page. I've even tried setting the content type to application/json:

headers = {'Content-Type': 'application/json'}
result = urlfetch.fetch('{}/users/{}.json'.format(domain, username), headers=headers)

What am I doing wrong?


Solution

  • Resolved:

    Need to add api_key and api_username to GET request:

    result = urlfetch.fetch('{}/users/{}.json?api_key={}&api_username={}'.format(domain, username, discourse_api_key, discourse_api_username))