Issues updating rows with Rlabkey

LabKey Support Forum
Issues updating rows with Rlabkey awilson  2024-05-09 09:47
Status: Active
 

hello, recently updated to Labkey 24.3 also updated the Rlabkey package (version: 3.2.1) to work with new release.

I can pull data (apiKey|selectRows) into RStudio from labkey server without issue but writing to the server is where issues arise. Trying to insert rows (insertRows|importRows) from R Studio and getting the a 401 error. Have done this in previous releases of labkey (pre-tomcat embed) with no issue.

CONTENTS of ERROR:
Error in handleError(response, haltOnError) :
HTTP request was unsuccessful. Status code = 401, Error message = This request has an invalid security context. You may have signed in or signed out of this session. Try again by using the 'back' and 'refresh' button in your browser.

 
 
mohara responded:  2024-05-09 13:38

Hi, and thanks for reaching out.

That error message implies a problem in authentication or security settings, and I can confirm writing to the server v24.3 from RStudio does work with appropriate permissions, so some troubleshooting questions:

  1. Can you clarify how you're structuring the selectRows and insertRows calls? Presumably you're using the same apiKey authentication with both (possibly via a netrc file?) though the way you typed the question doesn't make that clear.

  2. Assuming it's the same apiKey you confirmed that the apikey was generated from an account with permission to insert/import into the table? If that account only has read access, insert would not work of course. Any chance container or account permissions changed?

  3. And was it an apiKey or a sessionKey that possibly expired? Regenerating a new apiKey and trying that would be good.

I've tried a few things locally and while I can't reproduce the specific error you report seeing, I did notice some unexpected error messages when I simulated missing a step in the upgrade process:

When you upgraded to 24.3, had you been using a context path (such as "/labkey" if you deployed with a configuration file named "labkey.xml")? If so, your baseUrl in R scripts may include it (i.e. for a local dev machine, something like "http://localhost:8080/labkey/").
If you've now deployed at the root as recommended, try the insert again without that part of the baseUrl. If making that change makes the insert work, then confirm that your application.properties includes a legacyContextPath set to match the context path previously in use.
context.legacyContextPath=/labkey

Hopefully one of these ideas helps,

--Molly

 
awilson responded:  2024-05-10 08:51

yes of course. here is the code i am using. I have success running the code within network but outside of network does not work for insertRows. my .netrc may not be correct. i have it in the base home folder. should it be in the labkey home or the R home instead? Seems strange a pull would work outside network but not a push.

selectRows:
  labkey.data<-labkey.selectRows(labkey.setDefaults(apiKey="apikeyapikeyapikeyapikey"),
  baseUrl="http://xxx.xxx.xx.xxx:PORT", 
  folderPath="/this/path/study", 
  schemaName="lists", 
  queryName="data")


insertRows:
  labkey.data<-labkey.insertRows(labkey.setDefaults(apiKey="apikeyapikeyapikeyapikey"),
  baseUrl="http://xxx.xxx.xx.xxx:PORT", 
  folderPath="/this/path/study", 
  schemaName="lists", 
  queryName="otherDataSameJSON",
  toInsert=labkey.data)
 
mohara responded:  2024-05-10 12:52

I'm not sure what you mean about "within network" vs. "outside of network" - that could be making a difference?
As far as a 'pull' working but not a 'push', that seems like a permissions issue - can the account that apikey will authenticate both read and insert into that list? In your two examples, there are different queryName values, so maybe that account can read one and not write the other?

But meanwhile, you should not put that setDefaults call within the insertRows. I'm not actually sure why it would work in selectRows either (if it does indeed work there). Depending on the rest of the script you're using, you could place it outside/call setDefaults separately/first,

labkey.setDefaults(apiKey="apikeyapikeyapikeyapikey")

labkey.data<-labkey.insertRows(baseUrl="http://xxx.xxx.xx.xxx:PORT", 
...

OR using a netrc file would mean you didn't need the key itself in the script.

Where to place and how to make sure a netrc gets referenced varies by OS.

--Molly