title: How can I troubleshoot a 401 error from MS Dataverse through R?
body: <p>Yesterday I was using the following code to get data out of my MS Dataverse through R. It was working fine.</p>
  <pre><code>require(httr)
  require(rvest)
  
  dataverse_api = oauth_endpoint(request = NULL, 
     authorize = &quot;https://login.microsoftonline.com/REDACTED/oauth2/v2.0/authorize&quot;,
     access = &quot;https://login.microsoftonline.com/REDACTED/oauth2/v2.0/token&quot;,
     base_url = &quot;https://login.microsoftonline.com/common/oauth2/authorize?resourcehttps://org593dc393.crm4.dynamics.com&quot;)
  
  API.Key = &quot;REDACTED&quot;
  API.Secret = &quot;REDACTED&quot;
  
  App = oauth_app(&quot;EPS Project Development&quot;, key = API.Key, secret = API.Secret)
  
  API.token = oauth2.0_token(dataverse_api, App, scope = &quot;https://org593dc393.crm4.dynamics.com/.default&quot;)
  API.AuthKey = API.token$credentials$access_token
  
  GET.Buildings = GET(&quot;https://org593dc393.crm4.dynamics.com/api/data/v9.2/crfd0_dartbuildingses&quot;, add_headers(Authorization = paste(&quot;Bearer&quot;, API.AuthKey, sep = &quot; &quot;)))
  </code></pre>
  <p>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 <code>ipconfig/flushdns</code> and navigating directly to the endpoint and inspecting the request, which returned the following 200 response:</p>
  <p><a href="https://i.stack.imgur.com/X8Uls.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X8Uls.png" alt="enter image description here" /></a></p>
  <p>With all of these efforts, despite the fact that this code worked yesterday, I am still getting the following response in R:</p>
  <p><a href="https://i.stack.imgur.com/7PdwX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7PdwX.png" alt="enter image description here" /></a></p>
  <p>I also generated a new token using Postman. When I run the GET command using that token, I get a status of 200.</p>
  <pre><code>API.AuthKey = &quot;REDACTED TOKEN FROM POSTMAN&quot;
  GET.Buildings = GET(&quot;https://org593dc393.crm4.dynamics.com/api/data/v9.2/crfd0_dartbuildingses&quot;, add_headers(Authorization = paste(&quot;Bearer&quot;, API.AuthKey, sep = &quot; &quot;)))
  
  GET.Buildings$status_code
  [1] 200
  </code></pre>
  <p>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?</p>