osparamatrix / ks-orderapi-python

47 stars 59 forks source link

Invalid credentials #18

Open bhaskar219 opened 3 years ago

bhaskar219 commented 3 years ago

After trying to create client getting invalid credentials , unable to proceed.

client = ks_api.KSTradeApi(access_token = access_token, userid = userid ,consumer_key = consumer_key, ip = "", app_id = "") Traceback (most recent call last): File "", line 1, in File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\ks_api.py", line 24, in init session_init_res = ks_api_client.SessionApi(self.api_client).session_init(self.userid, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api\session_api.py", line 397, in session_init return self.session_init_with_http_info(userid, consumerKey, ip, appId, **kwargs) # noqa: E501 File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api\session_api.py", line 510, in session_init_with_http_info return self.api_client.call_api( File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 354, in call_api return self.__call_api(resource_path, method, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 173, in call_api raise e File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 166, in call_api response_data = self.request( File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 380, in request return self.rest_client.GET(url, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\rest.py", line 220, in GET return self.request("GET", url, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\rest.py", line 214, in request raise ApiException(status = r.status, reason = r.reason, body = r.data) ks_api_client.exceptions.ApiException: (401) Reason: Unauthorized HTTP response body: {"fault":{"code":900901,"message":"Invalid Credentials","description":"Access failure for API: /apim/session/1.0, version: 1.0 status: (900901) - Invalid Credentials. Make sure you have provided the correct security credentials"}}

VV1919 commented 3 years ago

hello @bhaskar219 , U have to provide app_id also.. it is provided to you when you register from this link below: https://www.kotaksecurities.com/itrade/user/marketing.exe?action=trade_api&req=92

Or scroll down to the bottom on this link and signup there. DO not use signup on top of the page. https://preferred.kotaksecurities.com/landing-page/api/

let me know how it worked out.

pv-ajay-thota commented 3 years ago

Hi, it did not work out. I still get Invalid Credentials. Do we need to base64 encode consumer_key or any other key? and where is the consumer_secret is used? I could see a settings.py. file. do we need to configure that before using it?


client = ks_api.KSTradeApi(access_token=access_token,
                           userid=userid,
                           consumer_key=consumer_key,
                           ip=ip,
                           app_id=app_id,
                           host=sandbox_env_host
                           )

all the variables are substitutes copied from this below url default application.

https://ctradeapi.kotaksecurities.com/devportal/applications/*******-****-*****-*****-*****/sandboxkeys/oauth

still I get this error.

image

please find the traceback.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\ks_api.py", line 24, in __init__
    session_init_res  =  ks_api_client.SessionApi(self.api_client).session_init(self.userid, \
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api\session_api.py", line 397, in session_init
    return self.session_init_with_http_info(userid, consumerKey, ip, appId, **kwargs)  # noqa: E501
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api\session_api.py", line 510, in session_init_with_http_info
    return self.api_client.call_api(
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api_client.py", line 354, in call_api
    return self.__call_api(resource_path, method,
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api_client.py", line 173, in __call_api
    raise e
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api_client.py", line 166, in __call_api
    response_data = self.request(
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\api_client.py", line 380, in request
    return self.rest_client.GET(url,
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\rest.py", line 220, in GET
    return self.request("GET", url,
  File "D:\ajay\Work\Projects\Trading_StockMarket\trade_venv\lib\site-packages\ks_api_client\rest.py", line 214, in request
    raise ApiException(status = r.status, reason = r.reason, body = r.data)
ks_api_client.exceptions.ApiException: (401)
Reason: Unauthorized
HTTP response body: {"fault":{"code":900901,"message":"Invalid Credentials","description":"Access failure for API: /apim/session/1.0, version: 1.0 status: (900901) - Invalid Credentials. Make sure you have provided the correct security credentials"}}
VV1919 commented 3 years ago

hello @pv-ajay-thota, did you get a email from :

tradeapi@kotaksecurities.com

in that email you can find the following credentials:

Username: ||||||||||||||||||||||||||||||
Password: |||||||||||||||||||||||||| AppId: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

can you also share your code, so that I can check??

VV1919 commented 3 years ago

my code:

import json
from ks_api_client import ks_api

credentials_file = open("credentials.json","r")
credentials = json.load(credentials_file)

credentials_file.close()

client = ks_api.KSTradeApi(access_token = credentials["accessToken"], userid = credentials["userID"], \
                 consumer_key = credentials["consumerKey"], ip = "127.0.0.1", app_id = credentials["appID"])

# Get session for user
client.login(password = credentials["password"])

#Generated session token
client.session_2fa(access_code = "1337") # replace 1377 with  the OTP u get when you click "generate access code" on web browser during login. its active for that whole day.

credentials.json file (insert your credentials instead of hashes)

{
    "accessToken":"#######-####-####-####-############",
    "userID": "##########",
    "password": "########",
    "consumerKey": "###################",
    "appID": "#############################"
}
pv-ajay-thota commented 3 years ago

Hi @VV1919 , I have received credentials from tradeapi@kotaksecurities.com.

image

for testing purpose, I have hardcoded string literals in the main script.

image

NOTE: earlier I tried with reference (opaque) token which is shown in your code example . now I have changed it to self contained JWT token in dev portal. still it didn't work.

pv-ajay-thota commented 3 years ago

Hi @VV1919, so I spoke to the Kotak trade API team and I got to know that I do not have access to the sandbox environment where I am trying to log in. and yeah for convenience while generating an access token we can set the expiry time to '-1' so that access_token never expires. so as of now, I can say it is resolved for me.

VV1919 commented 3 years ago

ohh okay @pv-ajay-thota. good to know.

bhaskar219 commented 3 years ago

Hi @VV1919

I did n't received any credentials through email , Searched in mail box multiple times.

How to proceed to get credentials ...

bhaskar219 commented 3 years ago

Hi @pv-ajay-thota ,

How we can reach kotak trade API team , Can you share details

bhaskar219 commented 3 years ago

Hi @VV1919

I did n't received any credentials through email , Searched in mail box multiple times.

How to proceed to get credentials ...

Its in spam . Thanks got it

bhaskar219 commented 3 years ago

Hi @VV1919 I did n't received any credentials through email , Searched in mail box multiple times. How to proceed to get credentials ...

Its in spam . Thanks got it

But still facing problem ,

client = ks_api.KSTradeApi(access_token = credentials["accessToken"], userid = credentials["userID"], \ ... consumer_key = credentials["consumerKey"], ip = "127.0.0.1", app_id = credentials["appID"]) Traceback (most recent call last): File "", line 1, in File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\ks_api.py", line 24, in init session_init_res = ks_api_client.SessionApi(self.api_client).session_init(self.userid, \ File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api\session_api.py", line 397, in session_init return self.session_init_with_http_info(userid, consumerKey, ip, appId, **kwargs) # noqa: E501 File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api\session_api.py", line 510, in session_init_with_http_info return self.api_client.call_api( File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 354, in call_api return self.__call_api(resource_path, method, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 173, in call_api raise e File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 166, in call_api response_data = self.request( File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\api_client.py", line 380, in request return self.rest_client.GET(url, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\rest.py", line 220, in GET return self.request("GET", url, File "C:\Users\bhaskaja\AppData\Local\Continuum\anaconda3\lib\site-packages\ks_api_client\rest.py", line 214, in request raise ApiException(status = r.status, reason = r.reason, body = r.data) ks_api_client.exceptions.ApiException: (403) Reason: Forbidden HTTP response body: {"fault":{"code":900908,"message":"Resource forbidden ","description":"Access failure for API: /apim/session/1.0, version: 1.0 status: (900908) - Resource forbidden "}}

So unable to proceed . Please help me how to reach you people.

VV1919 commented 3 years ago

hello @bhaskar219, after adding the credentials from the email, you have to generate an "access token" from application portal in kotak securities api website. usually the access token is valid for 3600 seconds but if u want to get a token valid for infinite time then while generating access token provide a negative value for duration eg: -1. use the newly generated token in your code and run it. your authentication problem should be solved with this. if not you can mail me at varunvivek96@gmail.com.

pv-ajay-thota commented 3 years ago

Hi @pv-ajay-thota ,

How we can reach kotak trade API team , Can you share details

ks.apihelp@kotak.com

sultanarun commented 3 years ago

Defining the host is optional and defaults to https://tradeapi.kotaksecurities.com/apim

//This is wrong you have to explicitly give host="https://ctradeapi.kotaksecurities.com/apim" should be given explicitly. in this function call client = ks_api.KSTradeApi You guys suck

prasadraju070 commented 3 years ago

my code:

import json
from ks_api_client import ks_api

credentials_file = open("credentials.json","r")
credentials = json.load(credentials_file)

credentials_file.close()

client = ks_api.KSTradeApi(access_token = credentials["accessToken"], userid = credentials["userID"], \
                 consumer_key = credentials["consumerKey"], ip = "127.0.0.1", app_id = credentials["appID"])

# Get session for user
client.login(password = credentials["password"])

#Generated session token
client.session_2fa(access_code = "1337") # replace 1377 with  the OTP u get when you click "generate access code" on web browser during login. its active for that whole day.

credentials.json file (insert your credentials instead of hashes)

{
    "accessToken":"#######-####-####-####-############",
    "userID": "##########",
    "password": "########",
    "consumerKey": "###################",
    "appID": "#############################"
}

WHERE CAN I find the app id i cant find it anywhere

kalilinux-png commented 3 years ago

video Tutorials

@sultanarun @pv-ajay-thota @bhaskar219 @VV1919 @prasadraju070 here is the helpful video https://youtu.be/JYQ0lDyIbwM