Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme example #63

Closed
sannegovaert opened this issue Mar 27, 2025 · 8 comments · Fixed by #64
Closed

Update readme example #63

sannegovaert opened this issue Mar 27, 2025 · 8 comments · Fixed by #64
Assignees
Labels
bug Something isn't working

Comments

@sannegovaert
Copy link
Member

The code in the README gives an error.

library(httr) # to talk to the internet
library(magrittr) # to use pipes
library(jsonlite) # to work with JSON files
library(askpass) # to safly enter a password in R

# To access the ETN database, we need a login (username + password). We'll ask
# for the password dynamically because that's safer than storing it as an object
username <- "<your username here!>"
# All functions can be adressed directly in the URL
endpoint <- "https://opencpu.lifewatch.be/library/etn/R/list_animal_ids"
# Request the result of the function to be a json, and put in a request
response <-
    httr::POST(paste(endpoint, "json", sep = "/"),
      body = list(
        credentials = glue::glue('list(username = "{username}", password = "{askpass::askpass()}")')
      )
    )
# Take the response of the server, and convert it into an R object we can use
response %>%
    httr::content(as = "text", encoding = "UTF-8") %>%
    jsonlite::fromJSON(simplifyVector = TRUE)
Error: lexical error: invalid char in json text.
                                       unused argument (credentials = 
                     (right here) ------^
@PietrH PietrH self-assigned this Mar 28, 2025
@PietrH PietrH added the bug Something isn't working label Mar 28, 2025
@PietrH
Copy link
Member

PietrH commented Mar 28, 2025

domain <- "https://opencpu.lifewatch.be/library/etnservice/R"
function_identity <- "list_animal_ids"
username <- "[email protected]"
httr2::request(domain) |>
  httr2::req_url_path_append(paste(sep = "/", function_identity, "json")) |>
  httr2::req_body_json(data = list(credentials = list(username = username,
                                          password = askpass::askpass()))) |>
  httr2::req_perform() |>
  httr2::resp_body_json(check_type = FALSE)

Also doesn't work for Sanne with a HTTP 400 return

@PietrH
Copy link
Member

PietrH commented Mar 28, 2025

@sannegovaert Could you try this in powershell just to validate your credentials?

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = @"
{
    `"username`": `"your_username`",
    `"password`": `"your_password`"
}
"@

$response = Invoke-RestMethod 'https://opencpu-test.lifewatch.be/library/etnservice/R/validate_login/json' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

@PietrH
Copy link
Member

PietrH commented Mar 28, 2025

Or Curl

curl --location 'https://opencpu-test.lifewatch.be/library/etnservice/R/validate_login/json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "your_username",
    "password": "your_pwd"
}'

@sannegovaert
Copy link
Member Author

@sannegovaert Could you try this in powershell just to validate your credentials?

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = @"
{
"username": "your_username",
"password": "your_password"
}
"@

$response = Invoke-RestMethod 'https://opencpu-test.lifewatch.be/library/etnservice/R/validate_login/json' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

The response is true.

@PietrH
Copy link
Member

PietrH commented Mar 28, 2025 via email

@sannegovaert
Copy link
Member Author

I had a typo in my username... So there is nog bug!

@sannegovaert
Copy link
Member Author

I am wrong, the inital code does not work but the new solution works :-)

@PietrH
Copy link
Member

PietrH commented Apr 1, 2025

Readme to be updated!

@PietrH PietrH changed the title Issue with logging in Update readme example Apr 1, 2025
@PietrH PietrH linked a pull request Apr 1, 2025 that will close this issue
@PietrH PietrH closed this as completed in #64 Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants