I'm trying to integrate Interactive Brokers (IBKR) OAuth 1.0a into my application. Currently, I'm stuck at the request token step (/oauth/request_token), where I receive a 401 error: invalid consumer.
Request Details:
- OAuth Consumer Key: TESTCONS (for testing)
- Signature Method: RSA-SHA256
- Callback URL: oob (out-of-band)
- Nonce and Timestamp: Generated dynamically
- Private Key: Loaded directly in the script
Code:
import requests
import time
import uuid
import urllib.parse
from requests_oauthlib import OAuth1
# API endpoint
url = ";
# OAuth parameters
oauth_params = {
"oauth_consumer_key": "TESTCONS",
"oauth_signature_method": "RSA-SHA256",
"oauth_timestamp": str(int(time.time())),
"oauth_nonce": uuid.uuid4().hex,
"oauth_callback": "oob"
}
# Load RSA private key (masked)
private_key = """-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBK... (masked)
-----END PRIVATE KEY-----"""
# Create OAuth1 object
auth = OAuth1(
client_key=oauth_params["oauth_consumer_key"],
signature_method=oauth_params["oauth_signature_method"],
rsa_key=private_key,
nonce=oauth_params["oauth_nonce"],
timestamp=oauth_params["oauth_timestamp"],
callback_uri=oauth_params["oauth_callback"]
)
# Make the request
response = requests.post(url, auth=auth)
# Check response
if response.status_code == 200:
credentials = urllib.parse.parse_qs(response.text)
print(f"Request Token: {credentials.get('oauth_token')[0]}")
print(f"Request Token Secret: {credentials.get('oauth_token_secret')[0]}")
else:
print(f"Error: {response.status_code}")
print(response.text)
Error Response
Error: 401
{"error":"id: 92177, error: invalid consumer","statusCode":401}
Update:
- Here's the link to the doc which I'm trying to follow:
Edit:
I want to use the Web API, the reason is to make a third party app, a platform where users can create algorithms and trade. So, basically the server will make trades on behalf of the users with their IBKR account.
I am trying to implement the Third-Party OAuth Flow with an individual IBKR account using the TESTCONS consumer key (as the documentation says it should work with individual account as well, "TESTCONS will be immediately usable for any registered client, regardless of whether the individual is applying for a first party or third party consumer key."). However, on the first step of requesting a temporary token with /request_token, I’m receiving a 401 error: invalid consumer in the response.
- I am utilizing the Web API (not the TWS API) because it offers a simplified, RESTful interface suitable for web-based applications and third-party integrations.
- The TESTCONS consumer key is designated for Paper Trading accounts.