Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

flattrade api - get access token - session key sample program

Last updated on 13 days ago
K
KevinMember
Posted 13 days ago
import requests
import hashlib

# Configuration
API_KEY = "7868b2c697y64cb76586adf7c4c03874"
API_SECRET = "2023.ecaf6c7cebe44e9d83c4e76597b2af7623316f4d1ca6c342"
AUTH_URL = "https://auth.flattrade.in/?app_key={api_key}" # Replace api_key dynamically
TOKEN_URL = "https://authapi.flattrade.in/trade/apitoken" # Endpoint for generating access token


def generate_access_token():
"""
Function to generate the access token by exchanging the request_code with the API.
"""
# Step 1: Provide the authentication URL
print(f"Open the following URL in a browser to authenticate:\n{AUTH_URL.format(api_key=API_KEY)}")

# Step 2: Enter the request code obtained from the redirect URL
request_code = input("Enter the request_code from the redirect URL: ")

# Step 3: Generate the SHA-256 hash
hash_value = hashlib.sha256((API_KEY + request_code + API_SECRET).encode()).hexdigest()

# Step 4: Prepare the payload
payload = {
"api_key": API_KEY,
"request_code": request_code,
"api_secret": hash_value
}

# Step 5: Make the API request to generate the token
print("Requesting access token...")
response = requests.post(TOKEN_URL, json=payload)

# Debugging Output
print("Response Status Code:", response.status_code)
print("Response Content:", response.text)

if response.status_code == 200:
try:
data = response.json()
if "stat" in data and data["stat"] == "Ok":
print("Access token generated successfully!")
print(f"Token: {data['token']}")
return data["token"]
else:
print(f"Error in API response: {data.get('emsg', 'Unknown error')}")
except Exception as e:
print(f"Error parsing response JSON: {str(e)}")
else:
print(f"Failed to generate access token. HTTP Status: {response.status_code}")
return None



# Main execution
if __name__ == "__main__":
token = generate_access_token()
if token:
print("Access Token:", token)
else:
print("Failed to generate an access token.")
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 3
Members Online 0

Total Members: 11
Newest Member: Jhilam