Apologies, this is my first time posting here. If I have not gone about it the right way please tell me! I have been following the documentation for setting up ODK central. I have tried to follow all steps exactly using these guidelines. I set up a server on digital ocean and I have my domain name.
I have been following the developer documentation on how to use the API. Below is the python script that I have been using:
I get this error (along with some other HTML):
"We're sorry but ODK Central doesn't work properly without JavaScript enabled. Please enable it to continue."
I really apologize but I have been trying for days and I cannot understand the problem!
Welcome, @lgorman, and thanks for taking the time to introduce yourself!
When you hit your website directly, you are accessing the Central frontend and indeed, it needs JavaScript. What you want to do is to access the backend by adding /v1/ between your base url and the endpoint you want to access.
Yes, every API call you make needs to specify what you will interact with. That’s what I was referring to as an “endpoint”. When you look at the docs, you should be able to use a drop down on the examples to get Python code.
Hi @lgorman! My knowledge of Python is limited, but I'm guessing that values is being converted to a string representation rather than to JSON. From a brief look into this, it seems that one option is to specify a json parameter (json=values) rather than a data parameter. Apparently an added benefit of this approach is that you don't need to specify the Content-Type header.
Hi @Matthew_White! Thanks for your responses Unfortunately now I get the following error message:
{"message":"Could not find the resource you were looking for.","code":404.1}
Which I do no understand as my login credentials are correct and I have modified the URL as describes above (i.e https://lgormanodk.co.uk/v1/sessions). Is there something I am missing? I can log into the website no problem and create app-users etc... I just don't seem to be able to connect with the API.
One thing that's interesting about Central is that the management website simply uses the API: the frontend has no privileged access to the data. That means that if you're able to log into the website, that's a sign that the API is working correctly. I think the next step is making sure the API request is formulated correctly.
I'm noticing in your first post that you specify requests.get(). However, to create a session, you need to send a POST request. Does it help if you specify POST instead of GET?
@Matthew_White@LN
Thank you so much for this! I am new to all this stuff and you both massively helped me!!! Hopefully one day I'll be able to help with something!
One thing to note is that in the API documentation, the python code uses urllib2. I am told the urllib2 is a "poisoned package" and that it is easier to use the requests library (see here).
For anyone who encounters the same issue here is the code that worked for me:
import os
import requests
# need to ensure that these variables are set in your environment
username=os.environ["odk_username"]
password=os.environ["odk_password"]
base_url="https://MYDOMAIN.co.uk/v1/sessions"
values ={"email":username,"password":password}
headers = {'Content-Type': 'application/json'}
request=requests.post(url=base_url,json=values,headers=headers)
print(request.text)
Hi, thank you your code. I try your code which above.It returned emails,project names.I just wondering ,can we get data for "submission" part of odk platform ?
Thank you for reply.actually I tryed this example over and over again but result code error: "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" :(( .Do you have an idea or anthing else,please ?I really don't understand what it happened. Thank you again
Without sharing the code you are using / your context it is not possible for me to tell. The code I shared is slightly adapted from Yaw's and needs to be further adapted as I have stored credentials in an external json file (even better in env. variables). Given the error message you get, I would understand that you are trying to load an empty JSON somewhere in your code
Just in case anyone is interested. I have also created some R-functions to work with the ODK central API. Hopefully these are useful to someone! Find them here.
That's some nice code, I like the clean and readable variable names! Would you be interested in contributing those functions not ruODK? (No drama if you prefer not to)
Hey that made my day in return!
I've created issues with checklists for all missing functions. Feel free to pick any one function (maybe a reading function because they're simpler to test) and create a PR. Happy to review your PR, you can submit a very early work in progress so I can review and help.
There's a few pointers in the contrib guide, and of course existing functions and tests are a good starting point re structure (documentation, authentication etc).