在 R 中运行 Azure 应用程序 - 身份验证问题

问题描述 投票:0回答:1

我正在通过 Github actions 在 R 中运行一个脚本,旨在执行一个虚拟操作(连接到 Sharepoint 位置并列出那里的所有文件)。我已在 Azure 中创建了一个新的应用程序注册,并为 Microsoft Graph 添加了正确的 API 权限。脚本如下:

library(AzureAuth)
library(Microsoft365R)
library(httr)

# Set environment variables for service principal authentication
tenant_id <- "my-tenant-id"
client_id <- "my-client-id"
client_secret <- "my-client-secret"

# Define the scope for the token
scope <- "https://graph.microsoft.com"  # For Microsoft Graph API

# Retrieve the Azure token using service principal credentials
token <- AzureAuth::get_azure_token(
  resource = scope,  # The scope/resource for which the token is required
  tenant = tenant_id,
  app = client_id,
  password = client_secret,
  auth_type = "client_credentials"
)

# Extract the access token
access_token <- token$access_token

# Use the token to authenticate to SharePoint
site <- Microsoft365R::get_sharepoint_site(
  site_url = "https://mysite.sharepoint.com/sites/SampleLocation",
  token = access_token
)

# Perform operations on SharePoint
drive <- site$get_drive()
drive$list_items()

当它在 Github 上运行时,它总是提示我 请将浏览器指向以下网址:,这会将我重定向到无效的 URL。根据 the Documentation ,我希望使用这个 auth_type ,不需要执行任何浏览器身份验证。我什至想避免这种情况,因为我稍后的目标是安排每天运行的操作,因此不需要执行任何手动身份验证。

r azure github github-actions azure-authentication
1个回答
0
投票

请注意,使用 Microsoft365R 功能时,用户交互是强制性的,因为它使用此处提到的委派权限。使用客户端凭据流生成的令牌不适用于这些功能。

当我通过 R Studio 在本地环境中运行您的代码时,它默认使用授权代码流程,并要求在浏览器中选择帐户进行登录,然后出现同意提示:

enter image description here

身份验证成功后,我会收到如下响应的 SharePoint 文件列表:

enter image description here

如果您想避免用户交互,您可以通过使用客户端凭据流生成令牌来调用 Microsoft Graph REST API 作为替代方案。

在我的例子中,我使用下面的修改过的代码来生成令牌并用它来获取SharePoint站点信息:

library(httr)
library(jsonlite)

tenant_id <- "tenantId"
client_id <- "appId"
client_secret <- "secret"

token_url <- paste0("https://login.microsoftonline.com/", tenant_id, "/oauth2/v2.0/token")

body <- list(
  client_id = client_id,
  scope = "https://graph.microsoft.com/.default",
  client_secret = client_secret,
  grant_type = "client_credentials"
)

response <- POST(
  url = token_url,
  body = body,
  encode = "form"
)

if (status_code(response) == 200) {
  cat("Token successfully retrieved.\n")
  
  token_data <- content(response, "parsed")
  access_token <- token_data$access_token
  
  graph_url <- "https://graph.microsoft.com/v1.0/sites/root:/sites/siteName"
  
  graph_response <- GET(
    graph_url,
    add_headers(
      Authorization = paste("Bearer", access_token)
    )
  )
  
  if (status_code(graph_response) == 200) {
    cat("Request successful. Full response data:\n")
    
    content_data <- content(graph_response, "parsed")
    
    print(content_data)
    
  } else {
    cat("Request failed with status code:", status_code(graph_response), "\n")
    content_data <- content(graph_response, "text")
    print(content_data)
  }
  
} else {
  cat("Failed to retrieve the token. Status code:", status_code(response), "\n")
  token_error <- content(response, "text")
  print(token_error)
}

回复:

enter image description here

您可以更改图表 URL 以获取该 SharePoint 站点中存在的驱动器列表:

graph_url <- "https://graph.microsoft.com/v1.0/sites/siteID/drives"

graph_response <- GET(
  graph_url,
  add_headers(
    Authorization = paste("Bearer", access_token)
  )
)

if (status_code(graph_response) == 200) {
  cat("Request successful. Full response data in JSON:\n")
  
  content_data <- content(graph_response, "parsed")
  json_response <- toJSON(content_data, pretty = TRUE, auto_unbox = TRUE)
  cat(json_response, "\n")
}

回复:

enter image description here

同样,使用以下代码通过更改图形 URL 来列出 SharePoint 文件信息:

graph_url <- "https://graph.microsoft.com/v1.0/drives/driveID/root/children"

graph_response <- GET(
  graph_url,
  add_headers(
    Authorization = paste("Bearer", access_token)
  )
)

if (status_code(graph_response) == 200) {
  cat("Request successful. Full response data in JSON:\n")
  
  content_data <- content(graph_response, "parsed")
  json_response <- toJSON(content_data, pretty = TRUE, auto_unbox = TRUE)
  cat(json_response, "\n")
}

回复:

enter image description here

参考:

列出文件夹的内容 - Microsoft Graph v1.0

© www.soinside.com 2019 - 2024. All rights reserved.