r - 尝试使用 R 连接到 Azure 表时出现 HTTP/1.1 400 错误

标签 r azure connection azure-table-storage

我是 R 新手。我正在寻找一个连接到 Azure 表的脚本,我在此线程中找到了一些有用的信息:Connecting to Azure Table Storage in R

但是,当我运行脚本时,我收到一个与发布问题的用户所遇到的错误非常相似的错误,并且我无法弄清楚出了什么问题。

这是我使用的代码(凭据已修改):

# 1B) Connects to an Azure Table based on the specifications for Shared Key: https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key

library(httr)
library(RCurl)
library(bitops)
library(xml2)

# Stores credentials in variable

Account <- "storageaccount"
Container <- "usage"
Key <- "key"


# Composes URL

URL <- paste0(
          "https://", 
          Account, 
          ".table.core.windows.net", 
          "/", 
          Container
        )

# Requests time stamp

requestdate <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "UTC" )

content_lenght <- 0

# Composes signature string

signature_string <- paste0(
                     "GET", "\n",                 # HTTP Verb
                     "\n",                        # Content-MD-5
                     "text/xml", "\n",            # Content-Type
                     requestdate, "\n",           # Date
                     "/", Account, "/", Container # Canonicalized resource
                    )

# Composes header string

header_string <- add_headers(
                    Authorization=paste0(
                      "SharedKey ", 
                      Account, 
                      ":",
                      RCurl::base64(
                        digest::hmac(
                          key = RCurl::base64Decode(
                            Key, mode = "raw"
                          ),
                          object = enc2utf8(signature_string),
                          algo = "sha256", 
                          raw = TRUE
                            )
                        )
                    ),
                    'x-ms-date' = requestdate, 
                    'x-ms-version' = "2022-01-15",
                    'Content-type' = "text/xml"
                  )

# Creates request

xml_body = content(
                GET(
                  URL, 
                  config = header_string, 
                  verbose()
                  )
                )

当我运行它时,我收到此错误:

-> GET /Usage HTTP/1.1
-> Host: storageaccount.table.core.windows.net
-> User-Agent: libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Authorization: SharedKey key
-> x-ms-date: Mon, 17 Jan 2022 15:47:30 UTC
-> x-ms-version: 2022-01-15
-> Content-type: text/xml
-> 
<- HTTP/1.1 400 The value for one of the HTTP headers is not in the correct format.
<- Content-Length: 371
<- Content-Type: application/xml
<- Server: Microsoft-HTTPAPI/2.0
<- x-ms-request-id: 0986de5d-9002-0064-1eb9-0b1166000000
<- Date: Mon, 17 Jan 2022 15:47:30 GMT
<- 
No encoding supplied: defaulting to UTF-8.

任何帮助将不胜感激。

另外,我不知道这是否相关,但我已经被提示几次安装 Rtools 包,但是当我搜索这个包时我找不到它。

提前致谢!

最佳答案

感谢您的帮助!

所以这是有效的代码:

library(httr)
library(RCurl)
library(bitops)
library(xml2)

# Stores credentials in variable

Account <- "storageaccount"
Container <- "Usage"
Key <- "key"


# Composes URL

URL <- paste0(
          "https://", 
          Account, 
          ".table.core.windows.net", 
          "/", 
          Container
        )

# Requests time stamp

requestdate <- format(Sys.time(), "%a, %d %b %Y %H:%M:%S %Z", tz = "GMT")


# As per Microsoft's specs, an empty line is needed for content-length

content_lenght <- 0

# Composes signature string

signature_string <- paste0(
                     "GET", "\n",                 # HTTP Verb
                     "\n",                        # Content-MD-5
                     "text/xml", "\n",            # Content-Type
                     requestdate, "\n",           # Date
                     "/", Account, "/", Container # Canonicalized resource
                    )

# Composes header string

header_string <- add_headers(
                    Authorization=paste0(
                      "SharedKey ", 
                      Account, 
                      ":",
                      RCurl::base64(
                        digest::hmac(
                          key = RCurl::base64Decode(
                            Key, mode = "raw"
                          ),
                          object = enc2utf8(signature_string),
                          algo = "sha256", 
                          raw = TRUE
                            )
                        )
                    ),
                    'x-ms-date' = requestdate, 
                    'x-ms-version' = "2020-12-06",
                    'Content-type' = "text/xml"
                  )

# Creates request

xml_body = content(
                GET(
                  URL, 
                  config = header_string, 
                  verbose()
                  ),
                "text"
                )

Get_data <- xml_body                             # Gets data as text from API  
From_JSON <-fromJSON(Get_data, flatten = TRUE)   # Parses text from JSON
Table_name <- as.data.frame(From_JSON)           # Saves data to a table

我添加了最后三行来解析 JSON 格式的数据并将其保存到表中。

关于r - 尝试使用 R 连接到 Azure 表时出现 HTTP/1.1 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70744445/

相关文章:

拨浪鼓R包: difference between risk and weight variable role?

r - 在 shinyapps.io 中部署 R 应用程序 - "Error:parsing manifest: Manifest file manifest.json checksum mismatch"

azure - 如何停止将过多的 ServiceBusReceiver.Receive 依赖项日志记录到 App Insights

azure - 在辅助角色中使用实例计数

java - 如何解决此通信链路故障?

arrays - 从多维数组返回一个 n×1 矩阵

r - 使用 R,通过从事件中减去年龄(以年为单位到 2SD)来计算出生日期

linux - 我应该迁移到 windows azure 以节省 magento 的成本吗?

c# - 为连接字符串动态提供用户名和密码

java - Spring data redis - 多模式 - 在 session 中更改模式(获取连接)VS。使用多个连接池