labkey python api access via netrc on a custom authenticated instance

LabKey Support Forum (Inactive)
labkey python api access via netrc on a custom authenticated instance vipin  2024-11-11 08:28
Status: Active
 

Dear LabKey support team,

We have a labkey instance where we use a first layer of authentication using a federated identity provider and we don't use the session token to authorise the user to LabKey instead we use the labkey default authentication as a second layer.

Now we would like to use the python api to such an instance and the url of the instance will redirect you to the identity provider to authenticate, how can we use the labkey specific authentication to access the api in such scenarios?

When I try to do the example python api access from the instance using a netrc file, I get the following error message:

Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/Users/scripts/data-management/labkey_data_parsers/clinical_data_parser/parse_redcap_csv.py", line 59, in <module>
main()
File "/Users/scripts/data-management/labkey_data_parsers/clinical_data_parser/parse_redcap_csv.py", line 50, in main
result = api.query.select_rows(schema, table)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scripts/data-management/labkey-data-management-env/lib/python3.12/site-packages/labkey/query.py", line 680, in select_rows
return select_rows(
^^^^^^^^^^^^
File "/Users/scripts/data-management/labkey-data-management-env/lib/python3.12/site-packages/labkey/query.py", line 462, in select_rows
return server_context.make_request(url, payload, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scripts/data-management/labkey-data-management-env/lib/python3.12/site-packages/labkey/server_context.py", line 192, in make_request
self._session.headers.update({CSRF_TOKEN: response["CSRF"]})
~~~~~~~~^^^^^^^^
KeyError: 'CSRF'

regards,
Vipin

 
 
mohara responded:  2024-11-15 14:15

Hi Vipin:

With the Community Edition, only our default database authentication is supported, so on this forum we can't help with what sounds like a more layered redirect scenario. It's also not clear to me what "example python api access" you are using from the above traceback, so I'd start simpler:

Generally, the first step with the Python API is a very basic script to just get the basics of your APIWrapper call correct. A great simple example of accessing the "Users" table is directly in the text of the README.md file here.

Troubleshooting ideas:

  • Try with both a real username/password combination and with an API key
  • You can try putting the credentials (in either form) directly in the script to confirm they work before using netrc to provide them
  • Documentation for APIWrapper is here, with some notes and links about CSRF protection and redirects, which are the first things that jump out about that traceback and your description.

Hopefully that helps you track this down,

--Molly