Basic authentication with the crul R package

,

Hi everyone,

Thanks for the really nice crul package. I would like to use it to create a R api package but I have an issue.
I tried to dig into the documentation with no success.

I want to know how to do basic http (or other methods) authentication like in Python requests or with R package httr.

Here are two examples of what I want to achieve with crul (let me know if it is right place such question or I should as it at SO).

import requests
from requests.auth import HTTPBasicAuth
requests.get("https://api.github.com/user", auth = ('username', 'password'))

Or with the package R httr

library(httr)
GET(url = 'https://api.github.com/user', authenticate(user = 'username', password = 'password'))

Thanks again for the support

Thanks for your question @dickoa

We didn’t have anything to support that yet - though you could do it with curl options yourself, but that’s a bit tedious if you aren’t familiar with them.

Reinstall devtools::install_github("ropensci/crul") and see ?auth

e.g.

library(crul)
res <- HttpClient$new(
  url = "https://httpbin.org/basic-auth/user/passwd",
  auth = auth(user = "user", pwd = "passwd")
)
res$auth
x <- res$get()
jsonlite::fromJSON(x$parse("UTF-8"))
2 Likes

Thanks a lot @sckott, it works perfectly.
Thanks again for this package it’s really a nice addition to the web stack in R.

Best,

glad it works :smiley:

Hi @sckott How would you go about the same thing with user and password requiring to be base64encode?

Appreciate your help

@OKohesta The crul package has been effectively retired, and rOpenSci now generally recommends the {httr2} package in place of crul. And I don’t think you can explicitly do what you’re asking with crul; see this issue, which auto-encodes all query parameters using curl::curl_escape(). {httr2} has straightforward ways to handle base64-encoded secrets. Hope that helps

1 Like

Hi @mpadge

It’s really sad to hear. I love the package and used it in several packages, some on CRAN.
Last time I checked, httr2 didn’t have all the features crul have, particularly for async requests. Is there an issue or a better place to discuss it, including how to transition if it required to do so.

Thanks,
Ahmadou

Sorry, I wrote without checking, but it does seem that Scott has once again been actively maintaining the package recently, so I guess if you open an issue on the GitHub repo he may respond.

1 Like

crul absolutely is not retired in any way. @dickoa I’m still maintaining it! Please do open any issues in that repo. I would say crul is stable at this point, but I can make fixes/add new features as time allows.

@OKohesta if you want to do this with crul please do open an issue over there. thanks!

2 Likes

I’m glad to hear that, and I’ll also see how to better support the package in the fure. I use httr2 for interactive and basic requests, but I still prefer crul to build API wrapper and R packages.
Thanks again for maintaining this package.

1 Like

Great to hear you’ll still use it!

I’m glad to hear you’re still maintaining it, sckott!

1 Like

@mpadge Thanks for the info will look into it. @sckott Thanks will do.

@sckott Sorry for my complete misunderstanding there :face_with_open_eyes_and_hand_over_mouth: I am also very pleased to know crul lives on :rocket: Thanks for all your awesome work!

Thanks Mark. And I see deposits is still going strong, thanks for taking that over :slight_smile:

I will say though that crul - like all my open source projects - doesn’t get as much attention as they did when I worked here since it’s in my free time, unless I can link it to work somehow :scheming:

1 Like