Request Bitbucket API with an access token - python

I'm trying to call Bitbucket API in my python script to retrieve some data.
I use the key/secret pair of Bitbucket OAuth.
data = { 'grant_type': 'client_credentials'}
response = requests.post('https://bitbucket.org/site/oauth2/access_token', data=data, auth=(key, secret))
print(response.json())
access_token = response.json()['access_token']
print(access_token)
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
print(groups.json())
This gives me an error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 35, in get_groups
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 306, in prepare
self.prepare_headers(headers)
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 438, in prepare_headers
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'
How do we use access token to call BitBucket API REST ?
EDIT
After correcting the header to json :
now it's saying
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 36, in get_groups
print(groups.json())
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

the headers parameter to the requests.get() should be an dictionary and you are sending set instead of the dictionary check your code it is headers={"Bearer %s" %access_token} which treated as set you can execute this on python and check.
You may need to use key:value check below example:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'access_token' : "Bearer %s" %access_token})

I successfuly get to retreive the data from Bitbucket API by passing the access token this way:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/access_token={%s}"%access_token)

Use Authorization as a key in the header instead of access_token try below code.
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'Authorization' : "Bearer %s" %access_token})

Related

Dash tutoriel error : Unexpected keyword argument

i'm learning Dash following this tutoriel https://github.com/amyoshino/Dash_Tutorial_Series
the problem is that when i install the project and try to run app.py function i found this error :
Traceback (most recent call last):
File "app.py", line 58, in <module>
dcc.Checklist(
File
"C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\
LocalCache\local-packages\Python38\site-packages\dash\development\base_component.py",
line 42, in wrapper
return func(*args, **kwargs)
File
"C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\
local-packages\Python38\site-packages\dash_core_components\Checklist.py", line 69, in __init__
super(Checklist, self).__init__(**args)
File
"C:\Users\Asus\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\
LocalCache\local-packages\Python38\site-packages\dash\development\base_component.py", line 82, in
__init__
raise TypeError(
TypeError: Unexpected keyword argument `values`
Allowed arguments: className, id, inputClassName, inputStyle, labelClassName, labelStyle,
loading_state, options, persisted_props, persistence, persistence_type, style, value
PS: I have installed all the requirements and libraries needed
when i change values with value the code runs but when i open the localhost another error occur
127.0.0.1 - - [17/Jan/2021 15:53:20] "←[35m←[1mGET /_dash-component-
suites/dash_renderer/dash_renderer.min.js.map?v=1.8.3 HTTP/1.1←[0m" 500 -
Traceback (most recent call last):
File
"C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File
"C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File
"C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1867, in
handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1952, in
full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1821, in
handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1950, in
full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1936, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File
"C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\
dash\dash.py", line 393, in serve_component_suites
mimetype = ({
KeyError: 'map'
127.0.0.1 - - [17/Jan/2021 15:53:21] "←[35m←[1mGET /_dash-component-
suites/dash_renderer/dash_renderer.min.js.map?v=1.8.3
HTTP/1.1←[0m" 500 -
Traceback (most recent call last):
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Asus\AppData\Local\
Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1867, in
handle_exception
reraise(exc_type, exc_value, tb)
File
"C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Asus\AppData\Local\
Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1952, in
full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1821, in
handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1950, in
full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\flask\app.py", line 1936, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Asus\AppData\Local\Packages\
PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-
packages\Python38\site-packages\dash\dash.py", line 393, in
serve_component_suites
mimetype = ({
KeyError: 'map'
127.0.0.1 - - [17/Jan/2021 15:53:21] "←[37mGET /favicon.ico HTTP/1.1←[0m" 200 -
That repo contains the error. As the traceback informs you, values is not allowed there, but you can use value. Change it locally and it should resolve the error. Additionally, you could make a pull request to that repo and help the original author(s) by fixing a bug in their code.

Connect via py2neo to a neo4j with password secured http protocol

Hello I am trying to connect to a neo4j database with py2neo.
This code works so far:
graph = Graph(bolt=True, host='***',
bolt_port=***,
http_port=***,
user='***',
password='***')
But when I protect my IP address with a password over HTTP then I can't connect and I don't know how to authorize py2neo to connect.
Does anyone know how I can solve this? :)
Edit :
When I open the ip in a web browser i need to input a user and a password before i can see the neo4j browser - and I do not now how to input these credentias with py2neo (because it seems that this is my problem)
Stacktrace:
Traceback (most recent call last):
File "WebApp35\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "WebApp35\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "WebApp35\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "WebApp35\lib\site-packages\flask\_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "WebApp35\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "WebApp35\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 271, in error_router
return original_handler(e)
File "WebApp35\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "WebApp35\lib\site-packages\flask\_compat.py", line 32, in reraise
raise value.with_traceback(tb)
File "WebApp35\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "WebApp35\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "WebApp35\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "WebApp35\lib\site-packages\flask_restful\__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "app\handlers\nodeHandlers.py", line 70, in get
return DataManager.get_suggestion(suggestion_string), 201, {
File "app\adapter\dataManager.py", line 44, in get_suggestion
return cls.adapter.get_suggestion(suggestion_string)
File "app\adapter\neoAdapter.py", line 337, in get_suggestion
for node in cls.cypher.run(query):
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 676, in run
return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 351, in begin
return self.transaction_class(self, autocommit)
File "WebApp35\lib\site-packages\py2neo\database\__init__.py", line 1171, in __init__
self.session = driver.session()
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\session.py", line 148, in session
session = Session(self)
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\session.py", line 461, in __init__
self.connection = connect(driver.host, driver.port, driver.ssl_context, **driver.config)
File "WebApp35\lib\site-packages\py2neo\packages\neo4j\v1\connection.py", line 399, in connect
raise error
py2neo.packages.neo4j.v1.exceptions.ProtocolError: Cannot establish secure connection; [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:720)

Peewee 'Connection already open' in url_value_preprocessor with Flask

What I am trying to do is check the subdomain value passed to the route methods just like explained here
Whenever I access the panel blueprint I receive this error: peewee.OperationalError: Connection already open
This is how I initialize the database in my main app file.
flask_db = FlaskDB(app)
database = flask_db.database
The problematic code seems to be
#panel.url_value_preprocessor
def check_client_and_user(endpoint, values):
client = Client.get(Client.subdomain == values.pop('client'))
The full source code can be found on Gitlab
__init__.py is the main app file. panel/__init__.py has the problematic code. This is the full traceback I receive:
Traceback (most recent call last):
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/playhouse/flask_utils.py", line 171, in connect_db
self.database.connect()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/peewee.py", line 3679, in connect
raise OperationalError('Connection already open')
peewee.OperationalError: Connection already open
I don't know where the extra connection is coming from. It seems like it is being generated by the Client.select() query.
Edit:
This code here
def get_or_404(client):
app.logger.debug(database.get_tables())
cli = Client.select().where(Client.subdomain == client)
Generates this response
--------------------------------------------------------------------------------
DEBUG in models [/home/marten/Projects/Comp/server/app/panel/models.py:14]:
['client', 'role', 'user', 'userroles']
--------------------------------------------------------------------------------
127.0.0.1 - - [17/Aug/2017 14:57:31] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/playhouse/flask_utils.py", line 171, in connect_db
self.database.connect()
File "/home/marten/Projects/Comp/server/venv/lib/python3.6/site-packages/peewee.py", line 3679, in connect
raise OperationalError('Connection already open')
peewee.OperationalError: Connection already open
As you can see the database is actually connected, and the client table is available.
Edit 2:
Well, I just found that apparently just database.get_tables() will cause this error. However, that code does fetch all tables from the database...
Does url_value_preprocessor run before the database is initialized? That would be strange since I import the object calling this decorator after initializing the database.

"TypeError: hashpw() argument 1 must be str, not bytes" when trying to register a user with flask-security

I'm trying to create a small flask application using the Enferno framework, but when I try to register a user I get an error that seems to be generated by the passlib library. I can't understand if it's something I did or if it is an error in the library itself.
Here's the full traceback:
Traceback (most recent call last):
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask_security/decorators.py", line 205, in wrapper
return f(*args, **kwargs)
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask_security/views.py", line 117, in register
user = register_user(**form.to_dict())
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask_security/registerable.py", line 28, in register_user
kwargs['password'] = encrypt_password(kwargs['password'])
File "/home/el3k0n/.local/lib/python3.4/site-packages/flask_security/utils.py", line 151, in encrypt_password
return _pwd_context.encrypt(signed)
File "/home/el3k0n/.local/lib/python3.4/site-packages/passlib/context.py", line 2495, in encrypt
return self._get_record(scheme, category).encrypt(secret, **kwds)
File "/home/el3k0n/.local/lib/python3.4/site-packages/passlib/utils/handlers.py", line 558, in encrypt
self.checksum = self._calc_checksum(secret)
File "/home/el3k0n/.local/lib/python3.4/site-packages/passlib/handlers/bcrypt.py", line 285, in _calc_checksum
return self._calc_checksum_backend(secret)
File "/home/el3k0n/.local/lib/python3.4/site-packages/passlib/utils/handlers.py", line 1458, in _calc_checksum_backend
return self._calc_checksum_backend(secret)
File "/home/el3k0n/.local/lib/python3.4/site-packages/passlib/handlers/bcrypt.py", line 333, in _calc_checksum_pybcrypt
hash = _bcrypt.hashpw(secret, config)
TypeError: hashpw() argument 1 must be str, not bytes
I just ran into this. Turns out, I had installed both bcrypt and python-bcrypt in my virtualenv. passlib.hash.bcrypt has a check to see which version on bcrypt is present. By removing the python-bcrypt, I was able to cease this error from being thrown.

Flask error "cannot concatenate 'str' and 'NoneType' objects" when POSTing multipart form data

So I'm using flask to receive form data, which has worked fine with url-encoded data (application/x-www-form-urlencoded). I can just access the request.form in my routing functions.
However if I try to instead send data using multipart/form-data, I consistently get the error: TypeError: cannot concatenate 'str' and 'NoneType' objects
This error occurs as soon as I try to access the request form, e.g. the line:
f = request.form. So it's nothing to do how I'm actually using the form data. It also happens regardless if I send the data from Fiddler, Postman or my own custom Java app.
Am I missing some code in flask to handle multipart form data, or is the request somehow invalid? Here's an example request from postman that causes the error:
And here's the full traceback (Locus is my app, so only line 18/19 are from my own code):
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask_debugtoolbar\__init__.py", line 125, in dispatch_request
return view_func(**req.view_args)
File "D:\Programming\VS\Visual Studio 2013\Projects\Locus Backend\Locus Backend\locus\Routing.py", line 67, in url_api_create
f = request.form
File "C:\Python27\lib\site-packages\werkzeug\local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "C:\Python27\lib\site-packages\werkzeug\utils.py", line 71, in __get__
value = self.func(obj)
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 483, in form
self._load_form_data()
File "C:\Python27\lib\site-packages\flask\wrappers.py", line 165, in _load_form_data
RequestBase._load_form_data(self)
File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 355, in _load_form_data
mimetype, content_length, options)
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 193, in parse
content_length, options)
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 99, in wrapper
return f(self, stream, *args, **kwargs)
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 208, in _parse_multipart
form, files = parser.parse(stream, boundary, content_length)
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 518, in parse
return self.cls(form), self.cls(files)
File "C:\Python27\lib\site-packages\werkzeug\datastructures.py", line 371, in __init__
for key, value in mapping or ():
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 516, in <genexpr>
form = (p[1] for p in formstream if p[0] == 'form')
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 475, in parse_parts
for ellt, ell in self.parse_lines(file, boundary, content_length):
File "C:\Python27\lib\site-packages\werkzeug\formparser.py", line 382, in parse_lines
next_part = b'--' + boundary
TypeError: cannot concatenate 'str' and 'NoneType' objects

Categories

Resources