flask: RunTimeError: There is no current event loop in thread - python

I am making a simple flask app that returns results from an external website. The user enters data into my site. This data is used to load another site. Data is extracted and returned in a list. This program works independently, but will not work as part of the flask app. I have tried using using requests_HTML library using it's async tools, and i have tried using Pyro4, to send the request to an external process. But i always come up with some version of this error:
RunTimeError: There is no current event loop in thread ........
It seems to me that when one of my imported modules runs a thread Flask doesn't like it.
I would love to know why does this happen, is their something in flasks inner workings that means that it won't work well with threads or async or something?
Any direction to some extra resources or info would be very appreciated
This is my Flask App:
from flask import Flask, render_template
from requests_html import AsyncHTMLSession
app = Flask(__name__)
asession = AsyncHTMLSession()
URLS=["https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/"]
#app.route("/", methods=('GET'))
def index():
results = asession.run(run_requests)
return render_template('index_page.html', results_list=results)
async def run_requests():
results_list=[]
for url in URLS:
results_list.append(await get_and_render(url))
return results_list
async def get_and_render(url):
r = await asession.get(url)
await r.html.arender(sleep=0.75)
summary = r.html.find(".question-hyperlink", first=True)
return summary.text
This will get the stackoverflow questions page and grab the summary of the last posted question. I have tried this code in a standalone python file and it works fine. (ie outside of a flask app, just printing the results to the command line)
This is by traceback from the flask debugger:
'Traceback (most recent call last):
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\......\StackOverflowExample.py", line 11, in index
results = asession.run(run_requests)
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\requests_html.py", line 771, in run
tasks = [
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\requests_html.py", line 772, in <listcomp>
asyncio.ensure_future(coro()) for coro in coros
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\asyncio\tasks.py", line 660, in ensure_future
loop = events.get_event_loop()
File "C:\Users\Alex\AppData\Local\Programs\Python\Python38-32\Lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-4'.
Can anyone explain why flask has trouble completing these tasks?
cheers
PS
here is my simple HTML (although app has not been able to render this yet.):
<ul>
{% for result in results_list %}
<li>{{result}}</li>
{% endfor %}
</ul>
Here is my Script outside the flask app(it works):
from requests_html import AsyncHTMLSession
asession = AsyncHTMLSession()
URLS=["https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/",
"https://stackoverflow.com/questions/"]
def index():
results = asession.run(run_requests)
for result in results:
print(result)
async def run_requests():
results_list=[]
for url in URLS:
results_list.append(await get_and_render(url))
return results_list
async def get_and_render(url):
r = await asession.get(url)
await r.html.arender(sleep=0.75)
summary = r.html.find(".question-hyperlink", first=True)
print(summary.text)
if __name__ == "__main__":
index()

Related

How to use asyncio with Flask?

Hello everyone I will explain my problem as clearly as possible. I have a Flask application and I want to retrieve the uid from the url settings and then check if the user exists in my Firebase Cloud Firestore using an await.
But I have an error, when my application has to process the same request several times in a short time, I have this error:
127.0.0.1 - - [27/Sep/2022 22:37:04] "GET /firestore/petale?uid=user123456789 HTTP/1.1" 200 -
[2022-09-27 22:37:08,883] ERROR in app: Exception on /firestore/petale?uid=user123456789 [GET]
Traceback (most recent call last):
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\Users\HP\PycharmProjects\flaskapp\routes\petale.py", line 14, in index
loop = asyncio.run(checkUid(uid))
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\Users\HP\PycharmProjects\flaskapp\routes\petale.py", line 21, in checkUid
uid_doc = await document.get()
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\google\cloud\firestore_v1\async_document.py", line 367, in get
response_iter = await self._client._firestore_api.batch_get_documents(
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\google\api_core\grpc_helpers_async.py", line 168, in error_remapped_callable
call = callable_(*args, **kwargs)
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\grpc\aio\_channel.py", line 168, in __call__
call = UnaryStreamCall(request, deadline, metadata, credentials,
File "C:\Users\HP\IdeaProjects\flaskapp\lib\site-packages\grpc\aio\_call.py", line 555, in __init__
self._send_unary_request_task = loop.create_task(
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 436, in create_task
self._check_closed()
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
127.0.0.1 - - [27/Sep/2022 22:37:08] "GET /firestore/petale?uid=user123456789 HTTP/1.1" 500 -
The highlighted error is: RuntimeError: Event loop is closed
You should know that I'm quite new to all the async/await methods, I come from Android with Java and it's not common for me. After several tests I could see that it comes from the line that retrieves the user's document with the await uid_doc = await document.get()
Searching on the internet I could see that people have this problem when they use the asyncio package, but I had not used it in my code. I decided to use it but I still have the same error and I don't know how to fix the problem. I don't really understand how to use asyncio.
import asyncio
from flask import Blueprint, request
from firebase_admin import firestore_async
petale = Blueprint("petale", __name__)
collection = firestore_async.client().collection('xxxx')
#petale.route("/petale", methods=['GET'])
def index():
uid = request.args.get('uid')
asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.run(checkUid(uid))
return loop
async def checkUid(uid):
document = collection.document(uid)
uid_doc = await document.get()
if uid_doc.exists:
return "Exists"
else:
return "Error"
I tried aioflask but there are many problems with it, I tried installing pip install flask[async] but it doesn't work either.
I'm a bit desperate, can you help me?
Thanks a lot!

`RuntimeError: Timeout context manager should be used inside a task` in flask api application

I'm working on exposing the results of a web scraper through a REST API running on a flask server.
Without going into too much detail, I have the following 2 files that call the same function that scrapes data from a site and returns it as json :
Working example
# testing_scraper_functions.py
scraper = Scraper(loginparams)
def run_task(task):
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(task())
# Simulating api call response
def searchChild(eiNumber):
return run_task(lambda: scraper.searchChild(eiNumber=eiNumber))
childSearchData = searchChild("776105")
scraper.quit()
Broken flask server example
app = Flask(__name__)
port = 5100
if sys.argv.__len__() > 1:
port = sys.argv[1]
scraper = Scraper(loginparams)
def run_task(task):
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(task())
#app.route('/api/search_ei/<eiNumber>')
def searchChild(eiNumber):
return run_task(lambda: hardWorker.searchChild(eiNumber=eiNumber))
if __name__ == '__main__':
app.run(host="0.0.0.0", port=port, debug=True)
scraper.quit()
They look like they should work the same but when I do the network request to the API I get the following Runtime Error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2095, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2080, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/Webapp/server.py", line 56, in searchChild
return run_task(lambda: hardWorker.searchChild(eiNumber=eiNumber))
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/Webapp/server.py", line 39, in run_task
return loop.run_until_complete(task())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/NYEIS_Scraper/NYEIS_API_V2/NYEIS_Worker.py", line 47, in wrapper
return await func(self, *args, **kwargs)
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/NYEIS_Scraper/NYEIS_API_V2/NYEIS_Worker.py", line 95, in searchChild
return (await self.searchChilds(eiNumber=eiNumber,
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/NYEIS_Scraper/NYEIS_API_V2/NYEIS_Worker.py", line 70, in searchChilds
html = await self._htmlFetcher.searchChild(eiNumber=eiNumber)
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/NYEIS_Scraper/NYEIS_API_V2/HTML_Fetcher.py", line 108, in searchChild
return await (await self._sessionHandler.fetch(method="post", url=url, payload=payload)).text()
File "/Users/c4q/Desktop/Pycharm Projects/NYEISNYEISBABY/NYEIS_Scraper/NYEIS_API_V2/SessionManager.py", line 56, in fetch
response = await self._session.post(url=url, headers=self.headers, data=payload)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/client.py", line 466, in _request
with timer:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task
In the error, NYEIS_worker is the Scraper
This scraping function is from an object that has a reference to an aiohttp ClientSession(), which is created when I initialize the object.
The only thing I can even see as a difference is that in the working example I call scraper.quit() which closes the ClientSession(), but I do that in the serverapp too, just after closing the server. Is the issue that the client session is open too long in the server example?
Should I create a scraper object and close it on every api request? I feel like this would be a lot slower. Should I reopen the clientsession() on every api call but keep the same scraper object? I'm not sure how to open a session again after closing, I would just be making a new one.
I'm not even sure what a Timeout context manager is. Can I use one without using async with? I've written everything using just await and running ClientSession().close() when I'm finished.
Any guidance here is appreciated.
I've kept most of the code out to keep my question shorter, but I can add more snippets if needed

Using requests_html in Google Cloud Functions or Run

I have a fairly basic scraping application that i want to run in a Google Cloud environment, i am using the requests_html Async library and it works fine in my local environment, however i cannot for the life of me figure out how to run it in Google Cloud having fiddled with it for days now.
The purpose of the application is to simply render some javascript pages (contained in the urls array) using html.arender and then with BeautifulSoup extract the content of some specific tags (from the tags array).
The error message i keep getting is:
"signal only works in main thread of the main interpreter"
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/functions_framework/__init__.py", line 99, in view_func
return function(request._get_current_object())
File "/workspace/main.py", line 53, in main
results = asyncio.run(collect(urls,tags))
File "/opt/python3.9/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/opt/python3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/workspace/main.py", line 32, in collect
return await asyncio.gather(*tasks)
File "/workspace/main.py", line 18, in getPage
await r.html.arender(timeout=40,sleep=1)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/requests_html.py", line 615, in arender
self.browser = await self.session.browser
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/requests_html.py", line 714, in browser
self._browser = await pyppeteer.launch(ignoreHTTPSErrors=not(self.verify), headless=True, args=self.__browser_args)
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/pyppeteer/launcher.py", line 307, in launch
return await Launcher(options, **kwargs).launch()
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/pyppeteer/launcher.py", line 159, in launch
signal.signal(signal.SIGINT, _close_process)
File "/opt/python3.9/lib/python3.9/signal.py", line 56, in signal
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread of the main interpreter
This is my code below:
from requests_html import AsyncHTMLSession
import asyncio
from bs4 import BeautifulSoup as bs
urls = ['list of urls']
tags = ['p','h1']
async def getPage(s, url):
r = await s.get(url)
await r.html.arender(timeout=60,sleep=3, scrolldown=2)
p = bs(r.html.html, "html.parser")
elmList = []
elmList.append(url)
for t in tags:
elements = p.findAll(t)
for e in elements:
elmList.append(e.text)
return elmList
async def collect(urls):
s = AsyncHTMLSession()
tasks = (getPage(s,url) for url in urls)
return await asyncio.gather(*tasks)
results = asyncio.run(collect(urls))
I have also attempted this same thing using the "non async" HTMLSession and rewriting the code to do one URL at a time, but i get the exact same error message in this case with the "signal only works in the main thread".
I have also tried to run this both in Cloud Functions and Cloud Run environment, with the same results.
Additionally, after trawling forums for advice have experimented with manually setting the loop like so, but this had no effect.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
If anyone has ideas for how to accomplish this with other libraries/methods please do let me know, it does not necessarily even have to be asynchronously, only requirement is javascript rendering of the pages.
Have you tried keep_page=True as a parameter for r.html.arender?
Why I'm asking: The error seems to happen when the browser that is used for rendering the JS is being shut down. Maybe keep_page=True avoids this.

NameError: name 'user_id' is not defined. For no apparent reason [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Error description
I would fire up the server program, and fire a request (GET) at the path /test_connection which is the path used by the client to test the server IP address btw. The server would then respond by printing out the traceback below in the console, the one line in particular that I can't get my head around is NameError: name 'user_id' is not defined as the variable is not even included in the response function for the path /test_connection (connection_test()). I know the error is on the server side as it will print out 'INTERNAL SERVER ERROR: 500' on the client code, so I didn't feel the necessity to include the client code.
Additional info:
Python version: 3.7
OS: Windows 10
Server Library: Flask
Traceback from error:
Traceback (most recent call last):
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\sccre\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\USB Stick Files\Programming\Python\Chat Room Thingy\server.py", line 19, in connection_test
message_dict[user_id]
NameError: name 'user_id' is not defined
Server code
from flask import Flask, request
from threading import Thread
import json
app = Flask(__name__)
message_dict = {}
ids = -1
#app.route('/test_connection')
def connection_test():
global ids
print('Connection tested from: {}'.format(request.remote_addr))
ids += 1
id_sent = str(ids)
return '{}'.format(id_sent)
#app.route('/new_message', methods=['POST'])
def new_message():
global message_dict
message_text = request.form['message']
user_id = request.form['id']
try:
message_dict[user_id]['message'] = message_text
except:
message_dict[user_id] = None
message_dict[user_id]['message'] = message_text
return '0'
#app.route('/chat')
def chat():
return json.dumps(message_dict)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Any help with this would be greatly appreciated.
Oscar.
Okay, The code is working completely fine to me.
Try one of the following :
1.Change your current File ( copy the contents to another )
2.Make the variable global.
3.Run the file directly from command prompt/shell (sometimes helpful)
4.Make sure your interpreter is ok

Oauth2 in Python with Flask gets 302 from API

I am trying to access MeisterTask's API with Python and Flask, and no matter what I do, I seem to always get a 302 code in return from the API, although I can get an access token (or so I think). Here is the code I have so far (I tried reducing it, this is the smallest snippet I could get that replicates the error):
from flask import Flask, redirect, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
app = Flask(__name__)
app.debug = True
app.secret_key = "development"
oauth = OAuth(app)
meistertask = oauth.remote_app(
'meistertask',
consumer_key= "XXXXXX",
consumer_secret= "XXXXXX",
request_token_params={"scope" : "meistertask"},
base_url='https://www.meistertask.com/api',
request_token_url=None,
access_token_method='GET',
access_token_url='https://www.mindmeister.com/login/oauth2/token',
authorize_url='https://www.mindmeister.com/oauth2/authorize'
)
#app.route('/')
def index():
if 'meistertask_token' in session:
me = meistertask.get('user')
return jsonify(me.data)
return redirect(url_for('login'))
#app.route('/login')
def login():
return meistertask.authorize(callback=url_for('authorized', _external=True))
#app.route('/logout')
def logout():
session.pop('meistertask_token', None)
return redirect(url_for('index'))
#app.route('/login/authorized')
def authorized():
resp = meistertask.authorized_response()
print(resp.get('code'))
if resp is None or resp.get('code') is None:
return 'Access denied: reason=%s error=%s resp=%s' % (
request.args['error'],
request.args['error_description'],
resp
)
session['meistertask_token'] = (resp['code'], '')
return "Hello"
#meistertask.tokengetter
def get_meistertask_oauth_token():
return session.get('meistertask_token')
if __name__ == "__main__":
app.run()
And here is the traceback:
flask_oauthlib.client.OAuthException: Invalid response from meistertask
Traceback (most recent call last):
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2295, in wsgi_app response = self.handle_exception(e)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise raise value
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 2292, in wsgi_app response = self.full_dispatch_request()
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\_compat.py", line 35, in reraise raise value
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request rv = self.dispatch_request()
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask\app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)
File "~\Documents\MeisterTaskServer\hello.py", line 49, in authorized resp = meistertask.authorized_response()
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_oauthlib\client.py", line 707, in authorized_response data = self.handle_oauth2_response(args)
File "~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\flask_oauthlib\client.py", line 692, in handle_oauth2_response
Things I have tried
Tried to modify the method to get the access token from GET to POST. The API clearly states that I should use GET, yet every other example I have seen on flask_oauthlib's GitHub uses POST (the examples are 3 years old, but some still work, namely the GitHub one). Either give the same result.
Tried doing it barebones, without any library. The resulting code was thrice as long and also had more problems.
Used Django instead of Flask. Never even managed to get the hello world example going, it was too much work, and also I have discovered the library flask_oauthlib.
Things worth mentioning
I derived this code from this here GitHub example
There is also code there I omitted in order to keep the snippet short, that establishes that the server should use SSL (as per the request from the API that the redirect_uri should use HTTPS)
The app manages to redirect me over at MeisterTask and asks for my permission. Once I grant it, it redirects to "https://127.0.0.1:5000/login/authorized?code=some_token" where I get the traceback. If I look with Chrome's debugging tools to the requests made and what I receive, I see that I get an 302 from the API, but I also get an access token.
I run Windows 10 with Python 3.7.0
So what's the deal? What's the next step here? I've run out of things to try. Thank you for taking the time to solve this!

Categories