Search code examples
rgetmicrosoft-graph-apihttp-status-code-401httr

How can I troubleshoot a 401 error from MS Dataverse through R?


Yesterday I was using the following code to get data out of my MS Dataverse through R. It was working fine.

require(httr)
require(rvest)

dataverse_api = oauth_endpoint(request = NULL, 
   authorize = "https://login.microsoftonline.com/REDACTED/oauth2/v2.0/authorize",
   access = "https://login.microsoftonline.com/REDACTED/oauth2/v2.0/token",
   base_url = "https://login.microsoftonline.com/common/oauth2/authorize?resourcehttps://org593dc393.crm4.dynamics.com")

API.Key = "REDACTED"
API.Secret = "REDACTED"

App = oauth_app("EPS Project Development", key = API.Key, secret = API.Secret)

API.token = oauth2.0_token(dataverse_api, App, scope = "https://org593dc393.crm4.dynamics.com/.default")
API.AuthKey = API.token$credentials$access_token

GET.Buildings = GET("https://org593dc393.crm4.dynamics.com/api/data/v9.2/crfd0_dartbuildingses", add_headers(Authorization = paste("Bearer", API.AuthKey, sep = " ")))

Today, trying to run the same lines of code with no modifications is giving me a 401 error. Based on some research on debugging 401 errors (none of them specific to MS Dataverse) I tried clearing my cache, clearing my cookies, flushing my DNS with ipconfig/flushdns and navigating directly to the endpoint and inspecting the request, which returned the following 200 response:

enter image description here

With all of these efforts, despite the fact that this code worked yesterday, I am still getting the following response in R:

enter image description here

I also generated a new token using Postman. When I run the GET command using that token, I get a status of 200.

API.AuthKey = "REDACTED TOKEN FROM POSTMAN"
GET.Buildings = GET("https://org593dc393.crm4.dynamics.com/api/data/v9.2/crfd0_dartbuildingses", add_headers(Authorization = paste("Bearer", API.AuthKey, sep = " ")))

GET.Buildings$status_code
[1] 200

Is it apparent to anyone in the R response to my initial attempt what I've done wrong? If not, how can I debug this?


Solution

  • It turned out the code was supplying the same Access Token each time. I just needed to add cache = FALSE so that I'm not using cached tokens. If it's helpful to anyone, the full code is now:

    require(httr)
    require(rvest)
    
    dataverse_api = oauth_endpoint(request = NULL, 
       authorize = https://login.microsoftonline.com/REDACTED/oauth2/v2.0/authorize,
       access = https://login.microsoftonline.com/REDACTED/oauth2/v2.0/token,
       base_url = https://login.microsoftonline.com/common/oauth2/authorize?resourcehttps://org593dc393.crm4.dynamics.com)
    
    API.Key = "REDACTED"
    API.Secret = "REDACTED"
    
    App = oauth_app("EPS Project Development", key = API.Key, secret = API.Secret)
    
    API.token = oauth2.0_token(dataverse_api, App, scope = https://org593dc393.crm4.dynamics.com/user_impersonation, cache = FALSE)
    API.AuthKey = API.token$credentials$access_token
    
    GET.Buildings = GET(https://org593dc393.crm4.dynamics.com/api/data/v9.2/crfd0_dartbuildingses, add_headers(Authorization = paste("Bearer", API.AuthKey, sep = " ")))