kmadac / bitstamp-python-client

Python package to communicate with bitstamp.net
MIT License
143 stars 70 forks source link

Invalid nonce error #18

Closed jasonmellone closed 6 years ago

jasonmellone commented 10 years ago

Hello,

I posted the question on stackoverflow, but I am getting the following error when using a valid user/key/secret.

http://stackoverflow.com/questions/24285109/python-bitstamp-api-invalid-nonce-error

Unsure what I can do to solve this or what I have done wrong. Please let me know if this is a bug or not as I would love to get it solved. Happy to contribute in modifying the code, but unsure why this is occurring.

This login was working up until it sporadically stopped working yesterday evening.

kmadac commented 10 years ago

Hello,

I just tried to test it, and account_balance() method works fine in my test script. Do you use account_balance() request in one thread only once per script run, or is it called in a loop many times per second or even in multiple threads?

Kamil

jasonmellone commented 10 years ago

Hello

Once per script. The script is run once an hour. 

I have a separate php site that reloads my balance and current level but that is once every few hours (manual). 

The website uses the php lib on git. No errors there.  — Sent from Mailbox

On Wed, Jun 18, 2014 at 4:26 PM, Kamil Madac notifications@github.com wrote:

Hello, I just tried to test it, and account_balance() method works fine in my test script. Do you use account_balance() request in one thread only once per script run, or is it called in a loop many times per second or even in multiple threads?

Kamil

Reply to this email directly or view it on GitHub: https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46489335

jasonmellone commented 10 years ago

Also I tried modifying the nonce get to just take the unix time not the max of nonce and unix time and I get the same error regardless. — Sent from Mailbox

On Wed, Jun 18, 2014 at 4:26 PM, Kamil Madac notifications@github.com wrote:

Hello, I just tried to test it, and account_balance() method works fine in my test script. Do you use account_balance() request in one thread only once per script run, or is it called in a loop many times per second or even in multiple threads?

Kamil

Reply to this email directly or view it on GitHub: https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46489335

kmadac commented 10 years ago

Is time on your server set correctly? Can you try generate new Bitstamp API key and secret and try your script with new credentials?

jasonmellone commented 10 years ago

It has the same error on a windows 8 pc and an up to date mac. I can easily generate a new key/secret. Will check back in once I do that. Thanks. — Sent from Mailbox

On Wed, Jun 18, 2014 at 5:03 PM, Kamil Madac notifications@github.com wrote:

Is time on your server set correctly?

Can you try generate new Bitstamp API key and secret and try your script with new credentials?

Reply to this email directly or view it on GitHub: https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46493753

SmileyChris commented 10 years ago

My guess is that you've used an nonce greater than the ones being generated by this script (maybe the phone nonce is bigger?) You can only ever use an nonce larger than the max of those used in the past. On Jun 19, 2014 9:21 AM, "jasonmellone" notifications@github.com wrote:

It has the same error on a windows 8 pc and an up to date mac. I can easily generate a new key/secret. Will check back in once I do that. Thanks. — Sent from Mailbox

On Wed, Jun 18, 2014 at 5:03 PM, Kamil Madac notifications@github.com wrote:

Is time on your server set correctly? Can you try generate new Bitstamp API key and secret and try your script

with new credentials?

Reply to this email directly or view it on GitHub:

https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46493753

— Reply to this email directly or view it on GitHub https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46495895 .

jasonmellone commented 10 years ago

Hm. Interesting but unsure. Maybe there is a discrepancy between the php nonce and the up nonce. 

— Sent from Mailbox

On Wed, Jun 18, 2014 at 6:09 PM, Chris Beaven notifications@github.com wrote:

My guess is that you've used an nonce greater than the ones being generated by this script (maybe the phone nonce is bigger?) You can only ever use an nonce larger than the max of those used in the past. On Jun 19, 2014 9:21 AM, "jasonmellone" notifications@github.com wrote:

It has the same error on a windows 8 pc and an up to date mac. I can easily generate a new key/secret. Will check back in once I do that. Thanks. — Sent from Mailbox

On Wed, Jun 18, 2014 at 5:03 PM, Kamil Madac notifications@github.com wrote:

Is time on your server set correctly? Can you try generate new Bitstamp API key and secret and try your script

with new credentials?

Reply to this email directly or view it on GitHub:

https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46493753

— Reply to this email directly or view it on GitHub https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46495895 .


Reply to this email directly or view it on GitHub: https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46501018

SmileyChris commented 10 years ago

Er phone -> php (thx autocomplete...)

jasonmellone commented 10 years ago

I knew what you meant no worries. I'll get a new key and give it a whirl. I will also try to recreate the error to be sure. — Sent from Mailbox

On Wed, Jun 18, 2014 at 6:12 PM, Chris Beaven notifications@github.com wrote:

Er phone -> php (thx autocomplete...)

Reply to this email directly or view it on GitHub: https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46501247

jasonmellone commented 10 years ago

Getting a new api key fixed the problem here. I will give the php a whirl later tonight to see if the issue is recreated.

Jason Mellone 631.219.5351 jason.mellone@gmail.com

On Wed, Jun 18, 2014 at 6:13 PM, Jason Mellone jason.mellone@gmail.com wrote:

I knew what you meant no worries. I'll get a new key and give it a whirl. I will also try to recreate the error to be sure. — Sent from Mailbox https://www.dropbox.com/mailbox

On Wed, Jun 18, 2014 at 6:12 PM, Chris Beaven notifications@github.com wrote:

Er phone -> php (thx autocomplete...)

— Reply to this email directly or view it on GitHub https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46501247 .

jasonmellone commented 10 years ago

Hello

I discovered and solved the issue for my own uses.

In the php api the following code is establishing the nonce: // generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems $mt = explode(' ', microtime()); $req['nonce'] = $mt[1] . substr($mt[0], 2, 6); $req['key'] = $key; $req['signature'] = $this->get_signature($req['nonce']);

with this, I have a comparison to the python code:

def get_nonce(self):

"""

Get a unique nonce for the bitstamp API.

This integer must always be increasing, so use the current unix time.

Every time this variable is requested, it automatically increments to

allow for more than one API request per second.

This isn't a thread-safe function however, so you should only rely on a

single thread if you have a high level of concurrent API requests in

your application.

"""

nonce = getattr(self, '_nonce', 0)

if nonce:

nonce += 1

If the unix time is greater though, use that instead (helps low

concurrency multi-threaded apps always call with the largest nonce).

self._nonce = max(int(time.time()), nonce)

return self._nonce

Which you are familiar with I am sure.

The error was indeed being created by the php/python piece of my cross language use.

A VERY VERY easy solution (I implemented this locally, but I will let you discuss if you want to apply it globally) is to chagnge:

self._nonce = max(int(time.time()), nonce)

to

self._nonce = max(int(time.time()*1000000), nonce)

As your nonce is created as:

1403366728.072785 and then cast to int

and the php nonce is created as:

1403366859731819

Do you think it would be best to update the python api to be in line with the php for cross functional purposes?

This is my first real interaction with a github project, so I am happy to to be wrong here.

Jason

Jason Mellone 631.219.5351 jason.mellone@gmail.com

On Thu, Jun 19, 2014 at 7:47 AM, Jason Mellone jason.mellone@gmail.com wrote:

Getting a new api key fixed the problem here. I will give the php a whirl later tonight to see if the issue is recreated.

Jason Mellone 631.219.5351 jason.mellone@gmail.com

On Wed, Jun 18, 2014 at 6:13 PM, Jason Mellone jason.mellone@gmail.com wrote:

I knew what you meant no worries. I'll get a new key and give it a whirl. I will also try to recreate the error to be sure. — Sent from Mailbox https://www.dropbox.com/mailbox

On Wed, Jun 18, 2014 at 6:12 PM, Chris Beaven notifications@github.com wrote:

Er phone -> php (thx autocomplete...)

— Reply to this email directly or view it on GitHub https://github.com/kmadac/bitstamp-python-client/issues/18#issuecomment-46501247 .

kmadac commented 10 years ago

Hi Jason,

thank you for investigation and sorry for delayed response. I think that it is not bad idea to generate nonce with microsecond precision. Could you please create pull request (https://help.github.com/articles/using-pull-requests) with your code change? It is easy and you will be become contributor of the Bitstamp client;)

kmadac commented 6 years ago

Closed because no reaction for a long time.