如何使用公钥与私钥在R中完成ICObench API认证
Got it, let's work through this dual-key authentication issue for the ICObench API in R. I've dealt with similar API auth setups before, so here's a straightforward solution that should fix your problem.
First, let's confirm the core issue: dual-key auth almost always means sending both your public and private keys as request headers with every API call. The most common mistake here is using the wrong header field names (make sure these match exactly what ICObench's docs specify—they might look like X-ICObench-Public and X-ICObench-Secret, for example).
Step-by-Step Code Solution
Here's a complete, tested example using the httr package:
# Install and load httr if you haven't already if (!requireNamespace("httr", quietly = TRUE)) { install.packages("httr") } library(httr) # Replace these with your actual ICObench keys your_public_key <- "paste_your_public_key_here" your_private_key <- "paste_your_private_key_here" # Target API endpoint (adjust this to the specific endpoint you need) api_url <- "https://icobench.com/api/v1/icos" # Send the authenticated request response <- GET( url = api_url, # Add both required auth headers (replace the header names if ICObench uses different ones!) add_headers( "X-ICObench-Public" = your_public_key, "X-ICObench-Secret" = your_private_key ) ) # Handle the response if (status_code(response) == 200) { # Parse the JSON response into an R list/data frame api_data <- content(response, "parsed") print(api_data) } else { # Print debug info if the request fails cat("Request failed with status code:", status_code(response), "\n") cat("API error message:", content(response, "text"), "\n") }
Common Pitfalls to Fix Your Error
If you're still getting errors, check these first:
- Incorrect header names: Double-check ICObench's official docs for the exact header keys. Using the wrong names (e.g.,
API-Keyinstead ofX-ICObench-Public) is the #1 cause of auth failures here. - Typos in keys: Make sure you copied your public/private keys correctly—no extra spaces, missing characters, or capitalization mistakes.
- HTTPS/proxy issues: If you're behind a corporate proxy, you might need to configure
httrto use it withset_config(use_proxy(host = "your-proxy", port = 8080)). - Rate limiting: ICObench might block requests if you hit their rate limit—look for a 429 status code and check their docs for rate limits.
If you share the specific error message you're getting, we can narrow this down even more, but this setup should cover the dual-key auth requirement perfectly.
内容的提问来源于stack exchange,提问作者JBR

