401 Client Error: Unauthorized for url: https://testnet.binancefuture.com/fapi/v1/order

I am using ccxt library for Python for creating a future order, on execution I am getting the error:

raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://testnet.binancefuture.com/fapi/v1/order

During handling of the above exception, another exception occurred:

Below is the code

exchange = ccxt.binance({
        'apiKey': API_KEY,
        'secret': SECRET,
        'enableRateLimit': True,
    })
    # exchange.set_sandbox_mode(True)
    params = {
        'test': True,  # test if it's valid, but don't actually place it
    }

    symbol = 'BTC/USDT'
    type = 'limit'  # or 'market'
    side = 'sell'  # or 'buy'
    amount = 1.0
    price = 0.060154  # or None

    exchange.options = {'defaultType': 'future', 'adjustForTimeDifference': True,'defaultTimeInForce':False}
    exchange.set_sandbox_mode(enabled=True)

    order = exchange.create_order(symbol, type, side, amount, price, params)

    print(order)

I even created a new API KEY Yet I am having the issue.

Do you have futures testnet account and using the testnet’s api key pair?
More details: Binance Testnet environments

Hi so I created new API at https://testnet.binancefuture.com/ and now when I am using them I am getting the error:

Traceback (most recent call last):
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/base/exchange.py", line 592, in fetch
    response.raise_for_status()
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://testnet.binancefuture.com/fapi/v1/order

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Me/Data/You/Missy/BillionX/python/trading/test_ccxt.py", line 44, in <module>
    order = exchange.create_order(symbol, type, side, amount, price, params)
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/binance.py", line 1875, in create_order
    response = getattr(self, method)(self.extend(request, params))
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/base/exchange.py", line 466, in inner
    return entry(_self, **inner_kwargs)
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/binance.py", line 2702, in request
    response = self.fetch2(path, api, method, params, headers, body)
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/base/exchange.py", line 487, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/base/exchange.py", line 608, in fetch
    self.handle_errors(http_status_code, http_status_text, url, method, headers, http_response, json_response, request_headers, request_body)
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/binance.py", line 2696, in handle_errors
    self.throw_exactly_matched_exception(self.exceptions, error, feedback)
  File "/Users/Me/Data/anaconda3/lib/python3.7/site-packages/ccxt/base/exchange.py", line 505, in throw_exactly_matched_exception
    raise exact[string](message)
ccxt.base.errors.BadRequest: binance {"code":-1115,"msg":"Invalid timeInForce."}

You need to disclose your full request details again. The previous one you posted shouldn’t trigger this error

Sorry I did not understand. What do you mean by disclosing full request? Here is my code:

exchange = ccxt.binance({
        'apiKey': API_KEY_FUTURE,
        'secret': SECRET_FUTURE,
        'enableRateLimit': True,
    })
    # exchange.set_sandbox_mode(True)
    params = {
        'test': True,  # test if it's valid, but don't actually place it
    }

    symbol = 'BTC/USDT'
    type = 'limit'  # or 'market'
    side = 'sell'  # or 'buy'
    amount = 1.0
    price = 0.060154  # or None

    exchange.options = {'defaultType': 'future', 'adjustForTimeDifference': True,'defaultTimeInForce':False}
    exchange.set_sandbox_mode(enabled=True)

    order = exchange.create_order(symbol, type, side, amount, price, params)

    print(order)

exchange.options = {‘defaultType’: ‘future’, ‘adjustForTimeDifference’: True,‘defaultTimeInForce’:False}
===>
exchange.options[‘defaultType’] = ‘future’

And you can overcome this issue. Also raise your price to a reasonable value or you’d be rejected by other rules

Sorry for being a dumb but how

exchange.options[‘defaultType’] = ‘future’
is different than

exchange.options = {'defaultType': 'future', 'adjustFo

But are valid Python dictionaries. I will try by increasing the price.

Hello, with exchange.options = {'defaultType': 'future', 'adjustForTimeDifference': True,'defaultTimeInForce':False} you’re re-defining the whole options and giving boolean to ‘defaultTimeInForce’, while it should be a string.

When this happens, it’s recommended to consult the source code, in this case: https://github.com/ccxt/ccxt/blob/master/python/ccxt/binance.py#L507

I changed as per your advice but still the same error. Here is the new code:

exchange.options['defaultType'] = 'future'
    exchange.options['adjustForTimeDifference'] = False
    exchange.options['defaultTimeInForce'] = 'GTC'
    exchange.set_sandbox_mode(enabled=True)

    order = exchange.create_order(symbol, type, side, amount, price, params)

    print(order)

Could you please run and check at your end?

I don’t think this would get you the same error and below two lines are totally unnecessary

 exchange.options['adjustForTimeDifference'] = False
 exchange.options['defaultTimeInForce'] = 'GTC'

Nevertheless it doesn’t matter. If you still cannot make it work, there might be something wrong with your python code and not API-related

It was price issue as mentioned. I set the price to 48K and it worked but when I ran the code again now it gives error:

ccxt.base.errors.ExchangeNotAvailable: binance {"code":-1016,"msg":"This service is no longer available."}

Is it possible I can cancel the previous order manually?

That means it gave you a different error message other than timeInForce! And I had already reminded you about that. Please be precise about the problem you encounter or we wouldn’t help you

What did you run again to trigger below error?

ccxt.base.errors.ExchangeNotAvailable: binance {"code":-1016,"msg":"This service is no longer available."}

I really apologize for that! The updated code was given below but it seems the issue with Binance testing servers because I was getting the same error on Web interface too. The following code is working fine now.

exchange = ccxt.binance({
        'apiKey': API_KEY_FUTURE,
        'secret': SECRET_FUTURE,
        'enableRateLimit': True,
    })
    # exchange.set_sandbox_mode(True)
    params = {
        'test': True,  # test if it's valid, but don't actually place it
    }

    symbol = 'BTC/USDT'
    type = 'limit'  # or 'market'
    side = 'sell'  # or 'buy'
    amount = 1.0
    price = 4810.12  # or None

    #exchange.options = {'defaultType': 'future', 'adjustForTimeDifference': True,'defaultTimeInForce':False}
    exchange.options['defaultType'] = 'future'
    exchange.options['adjustForTimeDifference'] = False
    #exchange.options['defaultTimeInForce'] = 'GTC'
    exchange.set_sandbox_mode(enabled=True)

    order = exchange.create_order(symbol, type, side, amount, price, params)

    print(order)

Thank you for all kind help.