HTTP 400 Error When Generating JWT Token


  • Hi everyone,

    I'm trying to integrate AngelOne with Google Sheets using Google Apps Script (GAS) to fetch market data. I'm stuck at the JWT token generation step.

    I'm using the generateTokens API endpoint (https://apiconnect.angelone.in/rest/auth/angelbroking/jwt/v1/generateTokens) with a POST request. I'm sending my API Key and Secret Key in the request payload as clientCode and password, respectively, with Content-Type set to application/JSON.

    However, I keep getting an HTTP 400 ("Bad Request") error. I've double-checked my API credentials, request payload, and headers, but I can't seem to find the issue.

    Here's the relevant part of my GAS code:

    // API Endpoint for generating JWT token
    var apiUrl = 'https://apiconnect.angelone.in/rest/auth/angelbroking/jwt/v1/generateTokens';
    
    // Request payload
    var payload = {
      "clientcode": apiKey, // Replaced with my actual API Key
      "password": secretKey  // Replaced with my actual Secret Key
    };
    
    // Make the API request
    var options = {
      'method': 'post',
      'contentType': 'application/json',
      'payload': JSON.stringify(payload)
    };
    
    var response = UrlFetchApp.fetch(apiUrl, options);
    

    I've also logged the payload and options to the Execution log, and they seem correct:

    Payload: {"clientcode":"my_actual_api_key","password":"my_actual_secret_key"}
    Options: {"method":"post","contentType":"application/json","payload":"{\"clientcode\":\"my_actual_api_key\",\"password\":\"my_actual_secret_key\"}"}
    

    I've tried testing with Postman, but I get the same 400 error.

    Can anyone help me figure out what's causing this error? Any suggestions or guidance would be greatly appreciated.

    Thanks!