Petersoj / alpaca-java

A Java API for Alpaca, the commission free, algo friendly, stock trading broker. https://alpaca.markets
https://petersoj.github.io/alpaca-java/
MIT License
202 stars 83 forks source link

Getting 401 trying to place paper trade buy order #156

Open s1rk1t opened 3 months ago

s1rk1t commented 3 months ago

Hi,

So I am able to query the market data API without issue using the example code you provided, but when I try to place a paper trade buy order I get the following error:

net.jacobpeterson.alpaca.openapi.trader.ApiException: Message: Unauthorized HTTP response code: 401 HTTP response body: {"code":40110000,"message":"request is not authorized"} HTTP response headers: {access-control-allow-credentials=[true], access-control-allow-headers=[Apca-Api-Key-Id, Apca-Api-Secret-Key, Authorization, Content-Type], access-control-allow-methods=[GET, OPTIONS, PUT, DELETE, POST, PATCH], access-control-allow-origin=[*], access-control-max-age=[1728000], connection=[keep-alive], content-length=[55], content-type=[application/json; charset=UTF-8], date=[Thu, 01 Aug 2024 00:11:49 GMT], strict-transport-security=[max-age=31536000; includeSubDomains], vary=[Origin], x-request-id=[8fe0e1ccf75ec4f0c42a613868e1ab5a]}

at net.jacobpeterson.alpaca.openapi.trader.ApiClient.handleResponse(ApiClient.java:1117)
at net.jacobpeterson.alpaca.openapi.trader.ApiClient.execute(ApiClient.java:1030)
at net.jacobpeterson.alpaca.openapi.trader.api.OrdersApi.postOrderWithHttpInfo(OrdersApi.java:842)
at net.jacobpeterson.alpaca.openapi.trader.api.OrdersApi.postOrder(OrdersApi.java:821)
at tests.Tests.testAlpacaBuyOrder(Tests.java:82)

I have tried it in different classes, but nothing seems to work. I'm using the format you gave in the README as follows:

final Order openingOrder = a.getAlpacaAPI().trader().orders() .postOrder(new PostOrderRequest() .symbol(symbol) .qty("1") .side(OrderSide.BUY) .type(OrderType.MARKET) .timeInForce(TimeInForce.GTC))

And since I'm able to query the market data API successfully, I don't believe there is any auth issue on my end since the AlpacaAPI object uses the same API key and secret for both.

Any idea what might be the issue?

Petersoj commented 3 months ago

A few ideas: