@@ -134,7 +134,8 @@ public class ApiClient {
134134
135135 private Gson json ;
136136
137- private Set <String > forbiddenHeaders = new HashSet <>(Arrays .asList ("host" , "authorization" , "cookie" , ":method" , ":path" ));
137+ private Set <String > forbiddenHeaders =
138+ new HashSet <>(Arrays .asList ("host" , "authorization" , "cookie" , ":method" , ":path" ));
138139
139140 public ApiClient (ClientConfiguration configuration ) {
140141 this (configuration , new BinanceAuthenticationFactory (), null );
@@ -188,8 +189,10 @@ public ApiClient(
188189 }
189190 }
190191
191- if (configuration .getCustomHeaders () != null && !configuration .getCustomHeaders ().isEmpty ()) {
192- Interceptor customHeadersInterceptor = getCustomHeadersInterceptor (configuration .getCustomHeaders ());
192+ if (configuration .getCustomHeaders () != null
193+ && !configuration .getCustomHeaders ().isEmpty ()) {
194+ Interceptor customHeadersInterceptor =
195+ getCustomHeadersInterceptor (configuration .getCustomHeaders ());
193196 builder .addInterceptor (customHeadersInterceptor );
194197 }
195198
@@ -217,13 +220,15 @@ public ApiClient(
217220 if (authentication != null ) {
218221 authentications .put (BINANCE_SIGNATURE , authentication );
219222 }
223+ }
220224
221- Authentication binanceApiKeyOnly =
222- (queryParams , headerParams , cookieParams , payload , method , uri ) -> {
225+ Authentication binanceApiKeyOnly =
226+ (queryParams , headerParams , cookieParams , payload , method , uri ) -> {
227+ if (signatureConfiguration != null && signatureConfiguration .getApiKey () != null ) {
223228 headerParams .put (HEADER_API_KEY , signatureConfiguration .getApiKey ());
224- };
225- authentications . put ( BINANCE_API_KEY_ONLY , binanceApiKeyOnly ) ;
226- }
229+ }
230+ } ;
231+ authentications . put ( BINANCE_API_KEY_ONLY , binanceApiKeyOnly );
227232 }
228233
229234 private void init () {
@@ -250,13 +255,15 @@ public void setJson(Gson json) {
250255
251256 public Interceptor getCustomHeadersInterceptor (Map <String , String > customHeaders ) {
252257 return chain -> {
253-
254258 Request request = chain .request ();
255259 Request .Builder newBuilder = request .newBuilder ();
256260 for (String headerName : customHeaders .keySet ()) {
257261 String headerValue = customHeaders .get (headerName );
258262 if (!validateHeader (headerName , headerValue )) {
259- throw new ApiException ("Invalid header " + headerName + ", it is forbidden or invalid (contains CR/LF)" );
263+ throw new ApiException (
264+ "Invalid header "
265+ + headerName
266+ + ", it is forbidden or invalid (contains CR/LF)" );
260267 }
261268
262269 newBuilder .addHeader (headerName , headerValue );
@@ -1413,9 +1420,22 @@ public Request buildRequest(
14131420
14141421 List <Pair > updatedQueryParams = new ArrayList <>(queryParams );
14151422
1423+ boolean hasAuth =
1424+ Arrays .stream (authNames )
1425+ .anyMatch (
1426+ s -> s .equals (BINANCE_SIGNATURE ) || s .equals (BINANCE_API_KEY_ONLY ));
1427+
1428+ // add api key to every request
1429+ String [] finalAuthNames ;
1430+ if (!hasAuth ) {
1431+ finalAuthNames = append (authNames , BINANCE_API_KEY_ONLY );
1432+ } else {
1433+ finalAuthNames = authNames ;
1434+ }
1435+
14161436 // update parameters with authentication settings
14171437 updateParamsForAuth (
1418- authNames ,
1438+ finalAuthNames ,
14191439 updatedQueryParams ,
14201440 headerParams ,
14211441 cookieParams ,
@@ -1862,4 +1882,13 @@ private Boolean validateHeader(String name, String value) {
18621882
18631883 return !value .contains ("\n " ) && !value .contains ("\t " );
18641884 }
1885+
1886+ private String [] append (String [] array , String value ) {
1887+ if (array == null ) {
1888+ return new String [] {value };
1889+ }
1890+ String [] newArray = Arrays .copyOf (array , array .length + 1 );
1891+ newArray [newArray .length - 1 ] = value ;
1892+ return newArray ;
1893+ }
18651894}
0 commit comments