flattrade / pythonAPI

15 stars 11 forks source link

AttributeError: NorenApiPy object has no attribute _NorenApi__username #1

Closed pravin2 closed 1 year ago

pravin2 commented 1 year ago

Getting Attribute Error from api_helper import NorenApiPy import credflt

api = NorenApiPy()

api.login(userid=credflt.user, password=credflt.pwd, twoFA=credflt.factor2, vendor_code=credflt.vc, api_secret=credflt.api_key, imei=credflt.imei)

print(api.get_limits())

Getting below error: AttributeError: 'NorenApiPy' object has no attribute '_NorenApi__username'

sanjurajin commented 1 year ago

Hi, Do not just copy the login code from finvasia. Flattrade has different method/code for this purpose. Go to the test_api.py and check the way to implement it. Good luck. Sanjau

pravin2 commented 1 year ago

I have followed the steps as defined in https://pi.flattrade.in/docs:

  1. I have created API Key from https://wall.flattrade.in/ where I have given redirect url as http://127.0.0.1?
  2. I pasted link https://auth.flattrade.in/?app_key=myapikey to browser and keyed in my Flattrade credentials. After that, I was redirected to blank page where I got below link on address bar http://127.0.0.1/?code=17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e&client=FTXXXXXX
  3. I copied the code and pasted myapikey and myapisecretkey along with in my Python code for POST method:
from api_helper import NorenApiPy
import requests
api = NorenApiPy()

data = '{"api_key":"myapikey", "request_code":"17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e&client=FTXXXXXX", "api_secret":"myapisecretkey"}'
response = requests.post('https://authapi.flattrade.in/trade/apitoken', data=data)
print(response)
api.set_session(userid= "FTXXXXXX", password = "XXXXXX", usertoken= response)
print(api.get_limits())

I am getting error:

<Response [200]>
{'stat': 'Not_Ok', 'emsg': 'Session Expired :  Invalid Session Key'}

Please let me know if anything needs to be corrected in the above sequence as I am not getting the result.

sanjurajin commented 1 year ago

I have followed the steps as defined in https://pi.flattrade.in/docs:

  1. I have created API Key from https://wall.flattrade.in/ where I have given redirect url as http://127.0.0.1?
  2. I pasted link https://auth.flattrade.in/?app_key=myapikey to browser and keyed in my Flattrade credentials. After that, I was redirected to blank page where I got below link on address bar http://127.0.0.1/?code=17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e&client=FTXXXXXX
  3. I copied the code and pasted myapikey and myapisecretkey along with in my Python code for POST method:
from api_helper import NorenApiPy
import requests
api = NorenApiPy()

data = '{"api_key":"myapikey", "request_code":"17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e&client=FTXXXXXX", "api_secret":"myapisecretkey"}'
response = requests.post('https://authapi.flattrade.in/trade/apitoken', data=data)
print(response)
api.set_session(userid= "FTXXXXXX", password = "XXXXXX", usertoken= response)
print(api.get_limits())

I am getting error:

<Response [200]>
{'stat': 'Not_Ok', 'emsg': 'Session Expired :  Invalid Session Key'}

Please let me know if anything needs to be corrected in the above sequence as I am not getting the result.

Yes, thats a wrong request code. you need to use only below : 17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e

do not include the texts after & sign i.e &client=FTXXXXXX. Hope it will solve the issue. do let me know if you still have problem. Sanju

pravin2 commented 1 year ago

As suggested, I tried in the market hours, but unfortunately no success. Getting same error. Any clues?

sanjurajin commented 1 year ago

you can check it any time (except 6.30 am to7.30 am, maintenance time; i suppose), it will work.

oh i didnt noticed it earlier. usertoken is not the response. as you mentioned like as below , is not right method.

~~data = '{"api_key":"myapikey", "request_code":"17284599fd003f7c.49f13ed680ceaa9f8566e7818c61ac10bf25410d9a8a29cea0b61fec3dfafa4e&client=FTXXXXXX", "api_secret":"myapisecretkey"}' response = requests.post('https://authapi.flattrade.in/trade/apitoken', data=data)~~

The sequence as follows :

  1. Generate the response code. (request_code in your case).
  2. after getting response Code string, you need to generate api_token.
  3. This api_token will be used as a usertoken to generate the session. (api.set_session in your case)

Suggestion : Go through this document And this documents Sanju.

pravin2 commented 1 year ago

Quite complicated login process Thanks for quick response. It worked.

sanjurajin commented 1 year ago

Quite complicated login process Thanks for quick response. It worked.

Hi, Good to know. Being a good contributor, Now you need to Close this issue. Good luck. Sanju