Search code examples
rmicrosoft365r

Connecting to Sharepoint using Microsoft365R error


I am trying to connect to sharepoint to load excel files within an unattended R script using the package Microsoft365R

I have created the app within Azure, and assigned it permissions. I have been able to successfully connect to a users onedrive and list the files within it.

Code that works

library(AzureGraph)
library(Microsoft365R)

tenant <- "your-tenant-here"

# the application/client ID of the app registration you created in AAD
# - not to be confused with the 'object ID' or 'service principal ID'
app <- "your-app-id-here"

# retrieve the client secret (password) from an environment variable
pwd <- Sys.getenv("EXAMPLE_MS365R_CLIENT_SECRET")

# retrieve the user whose OneDrive we want to access
# - this should be their 'userPrincipalName', which is of the form 'name@tenant.com'
# - note this may be different to their regular email address
user <- Sys.getenv("EXAMPLE_MS365R_TARGET_USER")

# create a Microsoft Graph login
gr <- create_graph_login(tenant, app, password=pwd, auth_type="client_credentials")

drv <- gr$get_user(user)$get_drive()

drv$list_files()

When running the below code, i get the error

# the application/client ID of the app registration to use
app <- "your-app-id-here"

# get the service account username and password
user <- Sys.getenv("EXAMPLE_MS365R_SERVICE_USER")
pwd <- Sys.getenv("EXAMPLE_MS365R_SERVICE_PASSWORD")

# SharePoint site and path to folder
sitename <- Sys.getenv("EXAMPLE_MS365R_SPO_SITENAME")
folderpath <- Sys.getenv("EXAMPLE_MS365R_SPO_FOLDERPATH")

# use the 'resource_owner' auth type for a non-interactive login
site <- get_sharepoint_site(sitename, tenant=tenant, app=app, username=user, password=pwd,
    auth_type="resource_owner")

Output:

Error in process_aad_response(res) : 
  Unauthorized (HTTP 401). Failed to obtain Azure Active Directory token. Message:
AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

The function doesn't appear to have an argument for client secret.

I have followed the vignettes within the package, but i must be missing something. Is anyone able to provide assistance?

Resource https://cran.r-project.org/web/packages/Microsoft365R/vignettes/scripted.html


Solution

  • You will get the AADSTS7000218 error when you try to get an authentication token for an application with the "Web" platform configuration without the client_secret parameter.

    How about when the platform configuration of the application to Mobile and desktop applications ?

    To change the platform configuration:

    • Open the application from App registrations page on Azure AD, and open Manage - Authentication page.
    • Delete the existing Web platform configuration.
    • Open Add a platform and select Mobile and desktop applications.