Search code examples
roauth-2.0powerappshttrdataverse

R httr MS Dataverse API: Bad Request (HTTP 400), Define scope


My question is closely related to this one: R httr Linkedin API: Bad Request (HTTP 400).

I have an application set up in Azure AD that I would like to use to get data out of MS Dataverse using R. I am new to APIs, so I first set it up in Postman. I was successful at getting to my data in Postman, but it did not require a Scope parameter.

I attempted to recreate what I did in Postman in R...

require(rvest)

dataverse_api = oauth_endpoint(request = NULL, 
   authorize = "REDACTED",
   access = "REDACTED",
   base_url = "REDACTED")

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)

This caused my browser to open and give me an error saying a scope was required. I have been searching through documentation to find nomenclature for scopes, but haven't been successful. I have found several documents (1, 2, etc.) that are somewhat peripheral that list options such as "ReadWrite.All" or "Files.ReadWrite.All", but my understanding is that these are permissions which are different from Scope. Because I don't know what I'm doing, I tried several of them as such:

require(rvest)

dataverse_api = oauth_endpoint(request = NULL, 
   authorize = "REDACTED",
   access = "REDACTED",
   base_url = "REDACTED")

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 = "User_impersonation")

The result is always the same. The browser opens with a message Authentication complete. Please close this page and return to R., but in R I get:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params,  : 
  Bad Request (HTTP 400). Failed to get an access token.

From the link I cited above as being a similar question, I understand that I am still missing the Scope parameter. But I do not know where to get the Scope parameter for MS Dataverse. Can anyone help with this?


Solution

  • I don't know if there are other options, but in case it's helpful to anyone else I was able to make it work by specifying the scope as the Dynamics 365 URL followed by .default. For me this looks like: https://org593dc393.crm4.dynamics.com/.default