title: Problems Connecting to MtGox API 2 with Python
body: <p>I am writing a trading program that I need to connect to MtGox (a bitcoin exchange) through the API v2. But I keep getting the following error:</p>
  <blockquote>
  <p>URL: <a href="http://pastebin.com/aXQfULyq" rel="nofollow noreferrer" title="here">1</a> <a href="https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address" rel="nofollow noreferrer">https://data.mtgox.com/api/2/BTCUSD/money/bitcoin/address</a></p>
  <p>HTTP Error 403: Forbidden.</p>
  </blockquote>
  <p>Most of my script is a direct copy from <a href="http://pastebin.com/aXQfULyq" rel="nofollow noreferrer" title="here">here</a> (that is a pastebin link). I just had to change it to work with Python 3.3.</p>
  <p>I suspect that it has to do with the part of script where I use base64.b64encode. <a href="http://pastebin.com/9fY8Jv15" rel="nofollow noreferrer">In my code</a>, I have to encode my strings to utf-8 to use base64.b64encode:</p>
  <pre><code>                url = self.__url_parts + '2/' + path
                  api2postdatatohash = (path + chr(0) + post_data).encode('utf-8')          #new way to hash for API 2, includes path + NUL
                  ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()).encode('utf-8'))
              
                  # Create header for auth-requiring operations
                  header = {
                       &quot;User-Agent&quot;: 'Arbitrater',
                       &quot;Rest-Key&quot;: self.key,
                       &quot;Rest-Sign&quot;: ahmac
                  }
  </code></pre>
  <p>However, with the other guy's script, he doesn't have too:</p>
  <pre><code>                url = self.__url_parts + '2/' + path
                  api2postdatatohash = path + chr(0) + post_data          #new way to hash for API 2, includes path + NUL
                  ahmac = base64.b64encode(str(hmac.new(base64.b64decode(self.secret),api2postdatatohash,hashlib.sha512).digest()))
             
                  # Create header for auth-requiring operations
                  header = {
                       &quot;User-Agent&quot;: 'genBTC-bot',
                        &quot;Rest-Key&quot;: self.key,
                       &quot;Rest-Sign&quot;: ahmac
                  }
  </code></pre>
  <p>I'm wondering if that extra encoding is causing my header credentials to be incorrect. I think this is another Python 2 v. Python 3 problem. I don't know how the other guy got away without changing to utf-8, because the script won't run if you try to pass a string to b64encode or hmac. Do you guys see any problems with what I am doing? Is out code equivalent?</p>