如何在R(或Curl)中签署OVH API

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

我尝试在 OVH 帐户上获取我的域名列表。

我尝试了如下代码


library(httr)
library(dplyr)
library(jsonlite)

urlAPI <- "https://ca.api.ovh.com/v1/domain"

GETendpoint <- "endpoint=ovh-eu"
GETappKey <- "&application_key=XX"
GETappSecret <- "&application_secret=XX"
GETconsKey <-  "&consumer_key=XX"

urlGET <- paste(urlAPI, "?", GETendpoint, GETappKey,GETappSecret,GETconsKey, sep = "")


OVH  <-
  GET(
    urlGET
  )

GET_OVH <-OVH %>% content("text") %>% fromJSON(flatten = F)

这给了我回应


[1] "You must login first"

如何登录 OVH 帐户并获取数据?

r curl ovh
1个回答
0
投票

我在该站点没有帐户,所以我无法真正测试,但查看他们提供的文档和代码示例,似乎您需要使用 sha1 签名来签署您的请求。我不知道有任何内置方法。假设您已按照此处所述创建了应用程序密钥 https://help.ovhcloud.com/csm/en-ca-api-getting-started-ovhcloud-api?id=kb_article_view&sysparm_article=KB0029722#advanced-usage-pair- ovhcloud-apis-with-an-application

这是一个参考函数,它将实现似乎与其文档一致的签名

OVH <- function(appKey, appSecret, consKey) {
  OVH_CALL <- function(method, url, query=NULL, data=NULL) {
    url <- httr::modify_url(url, query=query)
    body <- if (!is.null(data)) jsonlite::toJSON(data, auto_unbox=TRUE) else NULL
    now <- as.character(floor(as.numeric(Sys.time())))
    method <- toupper(method)
    signature <- digest::digest(paste(appSecret, consKey, "GET", url, body, now, sep="+"), algo="sha1", serialize=FALSE)
    headers <- httr::add_headers(
      "X-Ovh-Consumer" = consKey,
      "X-Ovh-Timestamp" = now,
      "X-Ovh-Signature" = signature
    )
    if (!is.null(data)) {
      headers$headers <- c(headers$headers, "Content-type"="application/json")
    }
    fun <- list("GET" = httr::GET, "POST"=httr::POST)
    fun[[method]](url, headers, body=body)
  }
  OVH_GET <- function(url, query=NULL) {
    OVH_CALL("GET", url, query=query)
  }
  OVH_POST <- function(url, query=NULL, data=NULL) {
    OVH_CALL("POST", url, query=query, data=data)
  }
  list(GET = OVH_GET, POST = OVH_POST)
}

此函数返回一个具有 GET 和 POST 方法的对象来处理签名。所以你会这样使用它

appKey <- "application_key"
appSecret <- "application_secret"
consKey <-  "consumer_key"
client <- OVH(appKey, appSecret, consKey)
client$GET("https://ca.api.ovh.com/v1/me")
client$GET("https://ca.api.ovh.com/v1/domain")
© www.soinside.com 2019 - 2024. All rights reserved.