简体   繁体   中英

Alpaca Traders API Unable to Connect with httplib

I am attempting to use the C++ wrapper for the Alpaca Traders API for the found here:

https://github.com/marpaia/alpaca-trade-api-cpp#client-instantiation

However, I'm having trouble even connecting to my paper trading account.

Here is the code from the wrapper for getting the Alpaca account:

httplib::Headers headers(const Environment& environment) {
  return {
      {"APCA-API-KEY-ID", environment.getAPIKeyID()},
      {"APCA-API-SECRET-KEY", environment.getAPISecretKey()},
  };
}

std::pair<Status, Account> Client::getAccount() const {
  Account account;

  httplib::SSLClient client(environment_.getAPIBaseURL());
  auto resp = client.Get("/v2/account", headers(environment_));
  if (!resp) {
    return std::make_pair(Status(1, "Call to /v2/account returned an empty response"), account);
  }
}

The problem is that I get an error back that it's unable to connect:

Error: resp.error(): Connection (2)

I've checked the environment, and it's been parsed correctly, I even tried the following curl command, and it was able to get the http page.

curl -X GET    -H "APCA-API-KEY-ID: {YOUR_API_KEY_ID}"    
-H "APCA-API-SECRET-KEY: {YOUR_API_SECRET_KEY}"   
https://paper-api.alpaca.markets/v2/account

So my machine can find, and get the page, thus it must be something in the code that is wrong. Any help would be appreciated.

I found the problem. After looking over the documentation on the cpp-httplib github , the SSLClient doesn't have the https:// at the beginning of the URL, and me having that in there was causing the problem.

So you want:

httplib::SSLClient client("paper-api.alpaca.markets");

and not:

httplib::SSLClient client("https://paper-api.alpaca.markets");

The second one will not connect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM