title: Getting Started with Square and Django
body: <p>I'm trying to get a simple Square credit card form to work on our Django website. I'm new to using Square, but have used similar API's such as Stripe in the past.</p>
  
  <p>To start, I followed along with their <a href="https://developer.squareup.com/docs/payment-form/payment-form-walkthrough" rel="nofollow noreferrer">Square Payment Form walkthrough</a>. I added their SqPaymentForm library and our own javascript file where I initialized a new SqPaymentForm.</p>
  
  <p>The front-end appears to be working and is generating a unique card nonce every time I enter fake credentials.</p>
  
  <p>Next, I submitted the form to our back-end (including the card nonce, in a hidden nonce field).</p>
  
  <p>On the back-end, I've installed the <a href="https://github.com/square/connect-python-sdk" rel="nofollow noreferrer">Square Connect Python SDK</a>. I copied their "Charge the card nonce" example as closely as possible, substituting our sandbox access token and location ID:</p>
  
  <pre><code>import uuid
  
  import squareconnect
  from squareconnect.rest import ApiException
  from squareconnect.apis.transactions_api import TransactionsApi
  
  # create an instance of the Transactions API class
  api_instance = TransactionsApi()
  # setup authorization
  api_instance.api_client.configuration.access_token = 'sandbox-access-token'
  
  location_id = 'sandbox-location-id'
  
  nonce = request.POST.get('nonce', 'empty')
  if (nonce == 'empty'): print("Throw error")
  # print(nonce)
  
  try:
    # Charge
    idempotency_key = str(uuid.uuid1())
    amount = {'amount': 100, 'currency': 'USD'}
    body = {'idempotency_key': idempotency_key, 'card_nonce': nonce, 'amount_money': amount}
    api_response = api_instance.charge(location_id, body)
    print (api_response.transaction)
  except ApiException as e:
    print ('Exception when calling TransactionApi-&gt;charge: %s\n' % e)
  </code></pre>
  
  <p>I've also tried reformatting this code slightly to fit the example demonstrated in Square's <a href="https://github.com/square/connect-api-examples/tree/master/connect-examples/v2/python_payment" rel="nofollow noreferrer">connect-api-examples on GitHub</a>.</p>
  
  <p>But, when I test it in localhost (http), using the different demo credentials provided by Square on their <a href="https://developer.squareup.com/docs/testing/test-values" rel="nofollow noreferrer">test values page</a>, I consistently get an 'Unauthorized' error from the API:</p>
  
  <pre><code>Exception when calling TransactionApi-&gt;charge: (401)
  Reason: Unauthorized
  HTTP response headers: HTTPHeaderDict(
  {'Content-Type': 'application/json', 'Vary': 'Origin, Accept-Encoding', 'X-Content-Type-Options': 'nosniff', 'X-Download-Options': 'noopen', 'X-Frame-Options': 'SAMEORIGIN', 'X-Permitted-Cross-Domain-Policies': 'none', 'X-Xss-Protection': '1; mode=block', 'Date': 'Wed, 31 Jul 2019 20:59:10 GMT', 'keep-alive': 'timeout=60', 'Strict-Transport-Security': 'max-age=631152000', 'content-length': '119'})
  HTTP response body: {"errors":[{"category":"AUTHENTICATION_ERROR","code":"UNAUTHORIZED","detail":"This request could not be authorized."}]}
  </code></pre>
  
  <p>When I look through the Troubleshooting documentation, it says the likely cause for an <a href="https://developer.squareup.com/docs/oauth-api/troubleshooting#unauthorized" rel="nofollow noreferrer">Unauthorized error</a> is an invalid OAuth token. Yet, none of Square's demos or examples utilize OAuth. I don't understand why OAuth would be necessary for a simple payment form that isn't behind a sign-up page?</p>
  
  <p>I've tried uploading the code to our https website to see if an SSL cert was required, but I get the same error.</p>