Search code examples
rapihttrqualtrics

Cant connect to Qualtrics API using httr package


I am trying to connect to Qualtrics API using Rstudio Cloud "httr" package to download mailing lists. After a review of the API documentation I was unable to download the data, getting the following error after running the code:

"{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Expected authorization in headers, but none provided.","errorCode":"ATP_2"},"requestId":"8fz33cca-f9ii-4bca-9288-5tc69acaea13"}}"

This does not makes me any sense since I am using a inherit auth from parent token. Here is the code:

    install.packages("httr")

    library(httr)

    directoryId<-"POOL_XXXXX"
    mailingListId <- "CG_XXXXXX"
    apiToken<-"XXXX"


    url<- paste("https://iad1.qualtrics.com/API/v3/directories/",directoryId,
             "/mailinglists/",mailingListId,"/optedOutContacts", sep = "")

   response <- VERB("GET",url, add_headers('X_API-TOKEN' = apiToken), 
   content_type("application/octet-stream"))

   content(response, "text") 

Any help will be appreciated.

Thanks in advance.


Solution

  • Your call to httr::VERB breaks the API token and the content type into two arguments to the function, but they should be passed together in a vector to a single "config" argument. Also, content_type isn't a function, it's just the name of an element in that header vector. This should work:

    response <- VERB("GET", url, add_headers(c(
                                   'X_API-TOKEN' = apiToken, 
                                   'content_type' = "application/octet-stream")))
    

    Note that mailing lists will be returned by Qualtrics as lists that will include both a "meta" element and a "result" element, both of which will themselves be lists. If the list is long, the only the first 100 contacts on the list will be returned; there will be an element response$result$nextpage that will provide the URL required to access the next 100 results. The qualtRics::fetch_mailinglist() function does not work with XM Directory contact lists (which is probably why you got a 500 error when using it), but the code for unpacking the list and looping over each "nextpage" element might be helpful.