0

I am trying to get the public token passed into my server (built in python flask). But I keep getting:

BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'public_token'

Here is my frontend written in jQuery:

onSuccess: async function(public_token, metadata) {
      // 2a. Send the public_token to your app server.
      // The onSuccess function is called when the user has successfully
      // authenticated and selected an account to use.
      await fetch('/get_access_token', {
        method: 'POST',
        body: JSON.stringify({ public_token: public_token }),
      });
    },

And the function that has problems in flask:

@app.route("/get_access_token", methods=['POST'])
def get_access_token():
  global access_token
  global item_id
  public_token = request.form['public_token']
  print(public_token)
  exchange_response = \
       client.Item.public_token.exchange(public_token)

  # Store the access_token and item_id in your database
  access_token = exchange_response['access_token']
  item_id = exchange_response['item_id']
  return jsonify(exchange_response)
  • in your javascript ```onSuccess```, shouldn't the body be: ```body: JSON.stringify({ "public_token": public_token}),```? – ewokx Jul 31 '20 at 07:24
  • if you send it as JSON then maybe you shouldn't get it as `request.form` but `request.data` or `request.json` – furas Jul 31 '20 at 08:53
  • https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask – Wereii Jul 31 '20 at 12:39

0 Answers0