Pages:
Author

Topic: Working Through the new MtGox Key & Secret API Authentication Model using C# - page 2. (Read 5035 times)

newbie
Activity: 29
Merit: 0
Be aware, that general misleading error: "{"error":"Must be logged in"}" will apply in every failed case except authorization for rights.

newbie
Activity: 29
Merit: 0
I signed in to help jimbobway on another thread but due to new user restrictions I can't answer him there.

http://forum.bitcoin.org/index.php?topic=29843.0


However, this is msg will probably help other C# coders trying to break through the learning curve since the new service can be a pain to authenticate to unless it's absolutely right. I lost the better half of the day to it but others have helped me in the past, so hope it helps...

Keep in mind, you'll have to supply your own Json Parser to the return string of Post

Code:
protected string Post( string moreargs = null ) {
// Verify client was instantiated with apikey and apisecret
if( string.IsNullOrEmpty( this.apiKey ) || string.IsNullOrEmpty( this.apiSecret ) )
throw new ArgumentException( "Cannot call private api's without api key and secret" );

// Create web request to mtgox

var parameters = "nonce=" + DateTime.Now.Ticks.ToString();
if( !string.IsNullOrEmpty( moreargs ) )
parameters += "&" + moreargs;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create( Url );

webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // you should probably key this with your client so they can help diagnose issues for you in their logs!!!
webRequest.Accept = "application/json";
// Required to authenticate on MtGox

webRequest.Headers["Rest-Key"] = this.apiKey;
webRequest.Headers["Rest-Sign"] = EncodeParamsToSecret( parameters );

byte[] byteArray = Encoding.UTF8.GetBytes( parameters );
webRequest.ContentLength = byteArray.Length;

using( Stream dataStream = webRequest.GetRequestStream() ) {
dataStream.Write( byteArray, 0, byteArray.Length );
}

using( WebResponse webResponse = webRequest.GetResponse() ) {
using( Stream str = webResponse.GetResponseStream() ) {
using( StreamReader sr = new StreamReader( str ) ) {
return sr.ReadToEnd();
}
}
}
}





Pages:
Jump to: