Title: Req: Python MtGox auth example Post by: osmosis on October 25, 2011, 06:37:56 AM The wiki has a mtgox authentication example in PHP. Anyone out there have an example they can provide for python? I have been trying to get goxsh.py to work with no luck.
https://en.bitcoin.it/wiki/MtGox/API Title: Re: Req: Python MtGox auth example Post by: kokjo on October 25, 2011, 06:52:50 AM Code: from urllib import urlencode Title: Re: Req: Python MtGox auth example Post by: osmosis on October 26, 2011, 05:36:27 AM THANKS!
Title: Re: Req: Python MtGox auth example Post by: kokjo on October 26, 2011, 07:42:56 AM THANKS! please consider to donate, so i am motivated to helping more people. :)Title: Re: Req: Python MtGox auth example Post by: molecular on January 10, 2012, 07:01:17 PM Code: from urllib import urlencode cool, this helped speed things up, thanks. Donation address? Title: Re: Req: Python MtGox auth example Post by: kokjo on January 10, 2012, 07:22:37 PM cool, this helped speed things up, thanks. 1JpsKmkim7REYq2EWE5aEXG8YN4zJTTX16Donation address? thanks :) Title: Re: Req: Python MtGox auth example Post by: osmosis on January 10, 2012, 07:27:13 PM THANKS! please consider to donate, so i am motivated to helping more people. :)thx kokjo Title: Re: Req: Python MtGox auth example Post by: kokjo on January 10, 2012, 07:34:28 PM im amazed it actually works. it was something i wrote up, in like 10 min.
Title: Re: Req: Python MtGox auth example Post by: molecular on January 11, 2012, 02:51:52 AM I'm also amazed.
looked at goxsh python impl a while back and I couldn't even figure out wtf was going on... your solution: drop 2 functions into my sources, change 2-3 lines => switched from user/pass to api key auth. thanks kokjo, sent a nickle Title: Re: Req: Python MtGox auth example Post by: finway on January 11, 2012, 05:58:22 AM mark.
Title: Re: Req: Python MtGox auth example Post by: Ente on January 28, 2012, 12:20:40 PM Code: from urllib import urlencode Thank you a lot for posting this! Uh, python newbie question: How to use that class? I tried: Quote key = "12345" (key and secret replaced)secret = "1234567890" path = "info.php" args = "123" tuer = requester(key,secret) tuer.perform(path,args) Dropped error: Quote Traceback (most recent call last): File "./auth.py", line 48, in <module> tuer.perform(path,args) File "./auth.py", line 33, in perform data, headers = self.build_query(args) File "./auth.py", line 24, in build_query req["nonce"] = get_nonce() TypeError: 'str' object does not support item assignment By the way, what goes in the "args" when none are needed, like when fetching "info"? Ah, success comes in tiny, bloody steps.. Ente Title: Re: Req: Python MtGox auth example Post by: kokjo on January 28, 2012, 12:44:58 PM Code: from urllib import urlencode Thank you a lot for posting this! Uh, python newbie question: How to use that class? I tried: Quote key = "12345" (key and secret replaced)secret = "1234567890" path = "info.php" args = "123" tuer = requester(key,secret) tuer.perform(path,args) Dropped error: Quote Traceback (most recent call last): File "./auth.py", line 48, in <module> tuer.perform(path,args) File "./auth.py", line 33, in perform data, headers = self.build_query(args) File "./auth.py", line 24, in build_query req["nonce"] = get_nonce() TypeError: 'str' object does not support item assignment By the way, what goes in the "args" when none are needed, like when fetching "info"? Ah, success comes in tiny, bloody steps.. Ente Title: Re: Req: Python MtGox auth example Post by: Ente on January 28, 2012, 01:31:37 PM args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work. Haha I really an a noob! :-) Thank you, kokjo! *flips a coin in your direction* Ente Title: Re: Req: Python MtGox auth example Post by: kokjo on January 28, 2012, 01:33:03 PM args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work. Haha I really an a noob! :-) Thank you, kokjo! *flips a coin in your direction* Ente Title: Re: Req: Python MtGox auth example Post by: molecular on January 28, 2012, 09:01:14 PM args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work. As much as I like python, but one has to admit that languages that use static typing do have certain advantages. Title: Re: Req: Python MtGox auth example Post by: Ente on January 31, 2012, 01:46:42 PM ah, here comes my next stumbling block:
(still Python, still with the code kokjo was so nice to post) How do I cancel an order? I only found old-api-examples (the apt where you POST your credentials directly). Is it even possible with the newer api? It expects: #POST data: oid=#&type=# #oid: Order ID #type: 1 for sell order or 2 for buy order For example: OID=123asd-123-etz-1542-12947 Type=1 123asd-123-etz-1542-12947=1 How do I pass this string into the "args" variable? Stupid.. everything else (get info, get orders, buy, sell) works as simple as passing the arguments as a JSON to the .php. Do I have to rewrite it all just for this single "cancel" order? Or is it only possible with the old API altogether, which will be deactivated in some weeks? ..being noobish again: Ente edit: I would expect something as easy as this to work: Code: path_cancel = "0/cancelOrder.php" Title: Re: Req: Python MtGox auth example Post by: kokjo on January 31, 2012, 02:03:03 PM Code: args = {"oid": "123-456-bla-bla", "type":"1"} notice the "" around the 1, AFAIK does urlencode not take numbers, only strings. Title: Re: Req: Python MtGox auth example Post by: Ente on January 31, 2012, 02:12:38 PM Yea, works, great! :-)
Actually I had the "1" in "", my mistake was even more silly. I did: Code: {"123-456-bla-bla": "1"} expected is: Code: {"oid": "123-456-bla-bla", "type":"1"} Thanks mate, again! Ente edit: wiki says Code: oid=#&type=# I just read that wrong. This is the moment where I really sense the value in programming languages. What the speaker says is what the listener will understand. Without two "but I meant.." in between! Title: Re: Req: Python MtGox auth example Post by: DBordello on February 10, 2012, 01:25:19 AM I am having difficulty getting this to work. Here is the code I am using. Any thoughts?
I am getting not logged in errors. Code: def test(): Title: Re: Req: Python MtGox auth example Post by: Waam on February 19, 2012, 05:17:06 AM I am having difficulty getting this to work. Here is the code I am using. Any thoughts? I am getting not logged in errors. Code: def test(): key =, etc are variables -- They need to be declared outside of a function. imports/froms should be before any other syntax. Let me know if you need any more help and I can post my working script (up to Authentication, I wouldn't want to do the work for ya.) ;) Title: Re: Req: Python MtGox auth example Post by: rokj on February 19, 2012, 03:45:36 PM https://github.com/rokj/bitcoin_dealer/blob/master/exchange/exchange/mtgox.py (https://github.com/rokj/bitcoin_dealer/blob/master/exchange/exchange/mtgox.py)
Here it is how I am doing, but it supports older API. Will implement support for newer API in few days. UPDATE: Here is for APIv1 https://github.com/rokj/bitcoin_dealer/blob/master/exchange/exchange/mtgox1.py (https://github.com/rokj/bitcoin_dealer/blob/master/exchange/exchange/mtgox1.py) Title: Re: Req: Python MtGox auth example Post by: rokj on February 22, 2012, 11:24:37 AM Following seems to work.
Code: params = [(u"nonce", self._create_nonce())] For some reason I could not get it working with MtGox APIv1 with other than with urllib3: Title: Re: Req: Python MtGox auth example Post by: TheStig on March 29, 2012, 01:18:13 AM A newbie wanted to post this
https://bitcointalk.org/index.php?topic=74530.0 I found this thread (http://'https://bitcointalk.org/index.php?topic=49789.20') to be extremely helpful, so I'm returning the favor by posting an improved and simplified version of the code. Unfortunately, my forum account is limited to the Newbie section. Mods, please move this post to the thread above. Code: from hashlib import sha512 Title: Re: Req: Python MtGox auth example Post by: Red Emerald on April 01, 2012, 07:17:09 AM args is a dict. args = {"someparameter": "value of the parameter"}, thats why it does not work. As much as I like python, but one has to admit that languages that use static typing do have certain advantages. Also, cython does allow setting types. Title: Re: Req: Python MtGox auth example Post by: Takkun on April 03, 2012, 08:07:57 PM Here is a full API v0 example using the authentication code posted earlier, if anyone is interested...
It's only missing withdraw, btcAddress, and history_[CUR]. Code: from urllib import urlencode Title: Re: Req: Python MtGox auth example Post by: jago25_98 on April 12, 2012, 03:27:45 AM appreciated. I'm actually a user looking for a command line method to do withdrawal codes,or at least a way to interact with arumxchange.
Title: Re: Req: Python MtGox auth example Post by: Nyx on April 13, 2012, 08:40:14 PM some newbie questions:
a) Why there is an API 0 and API 1. Will API 0 soon be deprecated? b) Is there a bug in the getTrades-Function? The parameter "since" seems to be ignored... Or is there an error in the code of Takkun? Thank you Title: Re: Req: Python MtGox auth example Post by: kokjo on April 13, 2012, 08:45:45 PM a) Why there is an API 0 and API 1. Will API 0 soon be deprecated? i think so.Title: Re: Req: Python MtGox auth example Post by: 029xue on April 17, 2012, 08:07:53 AM Thanks for the code.
However, when I try it, I always get the not logon error like below: Code: {u'error': u'Must be logged in'} The code is like below: Code: from urllib import urlencode The wired thing is, when I using the C# code to communicate, it all works well. I checked the http headers and found there're a little difference: .NET header Code: CONNECT mtgox.com:443 HTTP/1.1 Code: POST /api/0/info.php HTTP/1.1 Python header: Code: CONNECT mtgox.com:443 HTTP/1.0 Code:
The only difference is that: https://public.sn2.livefilestore.com/y1pvy4eqYCVgTia_7dDcpCIVy4wQ9-xxLR-VcM-NBvt1cGxzqiv7V2ijeAgimnTpXKXykAjrJlHo9Oz8DMasUL23g/123.png?psid=1 I guess it may related with the connection type, but I'm new to Python and have no idea how to control it... (why there're dozens of http lib exist in Python :) Any help is appreciate! Title: Re: Req: Python MtGox auth example Post by: rokj on April 17, 2012, 08:27:43 AM Thanks for the code. However, when I try it, I always get the not logon error like below: Code: {u'error': u'Must be logged in'} The code is like below: Code: from urllib import urlencode The wired thing is, when I using the C# code to communicate, it all works well. I checked the http headers and found there're a little difference: .NET header Code: CONNECT mtgox.com:443 HTTP/1.1 Code: POST /api/0/info.php HTTP/1.1 Python header: Code: CONNECT mtgox.com:443 HTTP/1.0 Code:
The only difference is that: https://public.sn2.livefilestore.com/y1pvy4eqYCVgTia_7dDcpCIVy4wQ9-xxLR-VcM-NBvt1cGxzqiv7V2ijeAgimnTpXKXykAjrJlHo9Oz8DMasUL23g/123.png?psid=1 I guess it may related with the connection type, but I'm new to Python and have no idea how to control it... (why there're dozens of http lib exist in Python :) Any help is appreciate! Had "similar" problems. First, use APIv1 and for some reason I got it working only with urllib3. See: https://bitcointalk.org/index.php?topic=49789.msg762709#msg762709 (https://bitcointalk.org/index.php?topic=49789.msg762709#msg762709). Title: Re: Req: Python MtGox auth example Post by: 029xue on April 17, 2012, 10:05:54 AM I just tried it. However, the result is same: ???
Code: import urllib, time, hashlib, hmac, base64, urllib3 May be because I'm under in proxy, but why .NET code work well? It's all in same machine... Title: Re: Req: Python MtGox auth example Post by: rokj on April 17, 2012, 10:12:20 AM I just tried it. However, the result is same: ??? Code: url = "https://mtgox.com/api/0/info.php" Try APIv1. https://mtgox.com/api/1/BTCUSD/public/ticker using GET or https://mtgox.com/api/1/generic/private/orders using POST. Title: Re: Req: Python MtGox auth example Post by: 029xue on April 17, 2012, 11:56:47 AM I just tried it. However, the result is same: ??? Code: url = "https://mtgox.com/api/0/info.php" Try APIv1. https://mtgox.com/api/1/BTCUSD/public/ticker using GET or https://mtgox.com/api/1/generic/private/orders using POST. Dose the code work on you side? If so, I may need start investigate my network problem instead of code... I got this "403 {"result":"error","error":"Identification required to access private API","token":"login_required"}" when try https://mtgox.com/api/1/generic/private/orders using POST. Thanks very much! Title: Re: Req: Python MtGox auth example Post by: rokj on April 17, 2012, 02:16:07 PM I just tried it. However, the result is same: ??? Code: url = "https://mtgox.com/api/0/info.php" Try APIv1. https://mtgox.com/api/1/BTCUSD/public/ticker using GET or https://mtgox.com/api/1/generic/private/orders using POST. Dose the code work on you side? If so, I may need start investigate my network problem instead of code... I got this "403 {"result":"error","error":"Identification required to access private API","token":"login_required"}" when try https://mtgox.com/api/1/generic/private/orders using POST. Thanks very much! Yes, I am using https://github.com/rokj/bitcoin_dealer/ (https://github.com/rokj/bitcoin_dealer/), you probably copied wrong key or missed something out. Title: Re: Req: Python MtGox auth example Post by: 029xue on April 20, 2012, 01:10:56 PM Thanks for your help. Your code is working and I finally find out the reason :)
My .NET code generate nonce like this: Code: string parameters = "nonce=" + DateTime.Now.Ticks.ToString(); In Python code, it generate nonce like this: Code: string parameters = "nonce=" + DateTime.Now.Ticks.ToString(); In like case, as it always smaller than the last .NET nonce in one account, then MtGox regards it as invalid and can't get authorization. I simply added more zero to "int(time.time()*1000000000)" and it then works. (or If I using one account for Python only, then it have no such problem also.) Again, thanks for your code :) Title: Re: Req: Python MtGox auth example Post by: rokj on April 20, 2012, 08:25:09 PM Thanks for your help. Your code is working and I finally find out the reason :) My .NET code generate nonce like this: Code: string parameters = "nonce=" + DateTime.Now.Ticks.ToString(); In Python code, it generate nonce like this: Code: string parameters = "nonce=" + DateTime.Now.Ticks.ToString(); In like case, as it always smaller than the last .NET nonce in one account, then MtGox regards it as invalid and can't get authorization. I simply added more zero to "int(time.time()*1000000000)" and it then works. (or If I using one account for Python only, then it have no such problem also.) Again, thanks for your code :) No problem! ;) Argh ... and totally forgot to write this frustration with nonce also (have had same problems when doing it in PHP and then in Python). Bold your message or something, I am sure someone else will also have the same experience in the future. :) |