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

error connecting to lotek site even with the sample password.- token supplied to the function is invalid #80

Open
dgermanwildlife opened this issue Aug 18, 2022 · 5 comments

Comments

@dgermanwildlife
Copy link

dgermanwildlife commented Aug 18, 2022

Here is my sample code:

lotek_base_url <- "https://webservice.lotek.com"
usr = "demo", pwd = "PASSWORD09"
httr::POST(url = lotek_base_url, path = list("API", "user", "login"), body = list(grant_type = "password", username = usr, Password = pwd))

Here is the response I get.

Error in curl::curl_fetch_memory(url, handle = handle) :
schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid

Looking online it may have to do with the Open SSL or the WinSSL security layers but I'm stymied as to how to get past this.

I opened the source code and used the httr commands to isolate which routine(s) contained the error.

Thanks for any help.

@Huh
Copy link
Owner

Huh commented Aug 18, 2022

@dgermanwildlife which version of the package are you using? The code above does not match the current version and for me returned a 400 error. The up to date version worked for me using:

lotek_base_url <- "https://webservice.lotek.com"

usr = "demo"

pwd = "PASSWORD09"

resp <- httr::RETRY(
  "POST",
  url = lotek_base_url,
  path = list("API", "user", "login"),
  body = list(
    grant_type = "password",
    username = usr,
    Password = pwd
  ),
  encode = "form",
  quiet = TRUE
)

httr::content(tt)
httr::http_error(tt)
httr::http_status(tt)

Please try the above code and perhaps try this as well to see if you can hit any website from your machine

RETRY("GET", "http://httpbin.org/status/200")

Last, any clues if you run lotek_login or the above code with something like:

httr::with_verbose(
  httr::RETRY(
    "POST",
    url = "https://webservice.lotek.com",
    path = list("API", "user", "login"),
    body = list(
      grant_type = "password",
      username = "demo",
      Password = "PASSWORD09"
    ),
    encode = "form",
    quiet = TRUE
  )
)

# Or maybe this

httr::with_verbose(
  collar::lotek_login(usr, pwd)
)

@dgermanwildlife
Copy link
Author

dgermanwildlife commented Aug 18, 2022 via email

@Huh
Copy link
Owner

Huh commented Aug 29, 2022

Turns out CO had a similar issue with ATS. Can you @dgermanwildlife try specifying the user_agent in your requests to see if this helps? I am travling yet again for the next two days, but will do my best to keep digging. Thanks.

Something like:

lotek_base_url <- "https://webservice.lotek.com"

usr = "demo"

pwd = "PASSWORD09"

resp <- httr::RETRY(
  "POST",
  url = lotek_base_url,
  path = list("API", "user", "login"),
  body = list(
    grant_type = "password",
    username = usr,
    Password = pwd
  ),
  encode = "form",
  quiet = TRUE,
  httr::user_agent(paste(
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
    "AppleWebKit/537.36 (KHTML, like Gecko)",
    "Chrome/103.0.0.0 Safari/537.36"
  ))
)

httr::content(resp)
httr::http_error(resp)
httr::http_status(resp)

@ericnewkirk
Copy link
Collaborator

@Huh
@dgermanwildlife

I've tried digging into this a bit, too, but can't reproduce it. All the Lotek functions are working as intended for me. There were curl updates a while back that may have been related, but the issues there seem a little off and are mostly out of date. I don't think it's related to the user agent issue with ATS, though.

I think a certificate is automatically being sent with the requests for some reason, maybe due to an existing login on the Lotek website that's been cached? I would try logging out of the Lotek website via browser and seeing if that makes a difference. It might also help to force the collar package to discard any existing login information by running lotek_logout() first:

library(collar)

lotek_logout()

usr <- "demo"
pwd <- "PASSWORD09"

lotek_login(usr, pwd)

alerts <- fetch_lotek_alerts()

lotek_logout()

I included a function in the package to refresh an existing token in case it's expired, but that only works sporadically and we should probably just get rid of it. If that's causing the issue then explicitly logging out as above should resolve it.

If neither of those works I'm afraid it might be something specific to your network configuration, but let's hope not. Given #81 it seems to affect requests to other urls too. As Josh suggested in the other thread

devtools::session_info()

could be helpful.

@Huh
Copy link
Owner

Huh commented Sep 3, 2022

Thanks @ericnewkirk

I am leaning towards a windows specific problem and the way certificates are passed. The schannel behavior in particular is well documented and if you look through issues for curl and libcurl there are mentions of this and a commit that gives the option to specify the cert explicitly. All of that is great, but if we can't reproduce it we are really just guessing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants