SpringBoot REST client is throwing error 403

lalit1980
Hi Team,

When I am trying to make a REST client to automate the login process, I am writing below method to call to get the request token.

import java.util.Arrays;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;


public class TestClient {

static final String URL_PROPERTIES = "https://kite.trade/connect/login?v=3&api_key=XXXXXXX";
public static void main(String[] args) {
testClient("old");

}

public static void testClient(String userId) {
// HttpHeaders
HttpHeaders headers = new HttpHeaders();

headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_XML }));
// Request to return XML format
headers.setContentType(MediaType.APPLICATION_XML);
// headers.set("my_other_key", "my_other_value");

// HttpEntity: To get result as Employee[].
HttpEntity entity = new HttpEntity(headers);

// RestTemplate
RestTemplate restTemplate = new RestTemplate();

// Send request with GET method, and Headers.
ResponseEntity response = restTemplate.exchange(URL_PROPERTIES, HttpMethod.GET, entity, String[].class);

HttpStatus statusCode = response.getStatusCode();
System.out.println("Response Satus Code: " + statusCode);

// Status Code: 200
if (statusCode == HttpStatus.OK) {
// Response Body Data
//String list = response.getBody();

response.getBody().toString();
}

}

}

But when I am running the programme I am getting below error.

15:08:02.223 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "https://kite.trade/connect/login?v=3&api_key=2hag9jvlagzb97ms"
15:08:02.237 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
15:08:03.123 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "https://kite.trade/connect/login?v=3&api_key=XXXXXXX" resulted in 403 (Forbidden); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 403 Forbidden

Can someone please help, do I need to have SSL certificate from Zerodha to https request?
  • lalit1980
    @sujith, @sujit123 Can you please help ? is there any way to write the Java REST client to get the Request Token by passing the api_secret?

    Do u have any REST client example to invoke?
  • sujith
    It is mandatory by the exchange that a user has to log in manually at least once a day. We don't recommend automating login.
  • lalit1980
    @sujith thanks for your response, yes I agree once in a day i need to do login but whenever I am trying to execute order program is throwing Invalid Token exception,

    So as per my understanding below are the steps I need to follow.

    Step 1: Login to https://kite.trade/connect/login?v=3&api_key=XXX and comple authentication process and acquire the request token and persist in DB or file or in memory.
    Question: Can I use same saved request token and access_token which I got it after generateSession method invoke or Every time manually I need to copy paste above URL and get request token and manually follow the same steps?

    Even if I need new request token all the time when I run my program can I use curl to invoke https://kite.trade/connect/login?v=3&api_key=XXX this url to get the request_token

    I am sorry if I am trying to ask stupid question but my plan is to make full process automated except once login and get the request token and use the same token all trading day.
  • sujith
    Once you get access token it is valid for one whole day.
Sign In or Register to comment.