You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R(或Curl)中签名OVH API并获取域名列表

解决OVH API登录认证问题并获取域名列表

你的代码核心问题是直接将密钥作为URL参数传递,OVH API要求使用请求签名机制完成身份认证,而非明文拼接密钥。以下是修正后的完整流程和代码:

1. 核心认证逻辑说明

OVH API的签名认证依赖以下要素生成合法请求头:

  • 当前Unix时间戳(X-Ovh-Timestamp)
  • 应用密钥(application_key)
  • 应用密钥(application_secret)
  • 已授权的消费者密钥(consumer_key)
  • 请求方法(如GET)
  • 请求目标URL

2. 修正后的R代码

library(httr)
library(jsonlite)
library(digest)

# 替换为你的实际密钥信息
application_key <- "XX"
application_secret <- "XX"
consumer_key <- "XX"
api_url <- "https://ca.api.ovh.com/v1/domain"

# 获取当前Unix时间戳(需与OVH服务器时间同步,偏差过大将导致签名验证失败)
timestamp <- as.character(as.integer(Sys.time()))

# 生成OVH要求的签名字符串
signature_string <- paste0("$1$", digest::hmac(
  key = paste0(application_secret, consumer_key),
  object = paste0(application_key, "+", consumer_key, "+", "GET", "+", api_url, "+", timestamp),
  algo = "sha1",
  raw = TRUE
))

# 发送带认证头的GET请求
response <- GET(
  url = api_url,
  add_headers(
    `X-Ovh-Application` = application_key,
    `X-Ovh-Timestamp` = timestamp,
    `X-Ovh-Consumer` = consumer_key,
    `X-Ovh-Signature` = signature_string,
    `Content-Type` = "application/json"
  )
)

# 解析并输出结果
result <- content(response, "text") %>% fromJSON(flatten = FALSE)
print(result)

3. 重要注意事项

  • 确保消费者密钥已完成授权:新创建的consumer_key需要通过OVH提供的授权链接,使用你的OVH账号登录并授予对应权限,否则仍会提示认证失败。
  • 时间同步:如果本地时间与OVH服务器时间偏差超过5分钟,签名会验证失败,可调用https://ca.api.ovh.com/v1/auth/time端点获取服务器标准时间。
  • 请求方法匹配:不同API端点对应不同请求方法(如GET/POST/PUT),生成签名时要严格匹配实际使用的方法。

内容的提问来源于stack exchange,提问作者Jan Jankowski Janek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:36:05