Hello,

I have managed to reproduce your issue and solve it. I have noticed two 
points of improvement that I would like to address so that you can move 
forward with your application.

When you run a `pip install` command in your shell, it is only installing 
the package on your local machine unless you are working in a virtual 
environment (which is recommended) and uploading the "venv" folder (which 
shouldn't be done).
In order to install a dependency on GAE, you should use the "requirements.txt" 
file <https://pip.pypa.io/en/stable/user_guide/#requirements-files>. This 
will allow GAE to be aware of the application's dependencies and install 
them by running `pip install -r requirements.txt`. This is explained in the 
documentation 
<https://cloud.google.com/appengine/docs/standard/python3/specifying-dependencies>.
 
In order to fix your issue I added the `flask_httpauth` library in the 
"requirements.txt" file.

Apart from that, I have noticed that the `flask_httpauth` is not properly 
imported in the code you provided. That causes your code to fail to execute 
even if the right dependency is installed. You can verify this by running 
`python main.py` in your local machine. In order to fix that issue I have 
changed the following line:

    from Flask_HTTPAuth import HTTPBasicAuth

By this one:

    from flask_httpauth import HTTPBasicAuth

After doing those changes I deployed to GAE and I was able to use your code 
to authenticate myself and properly access the `/rest-auth` endpoint.

Let us know if we can be of any further help.

Best regards,

Àlex J.
On Monday, February 1, 2021 at 9:50:24 AM UTC+1 [email protected] 
wrote:

> Hi all, 
>
> My GAE flask web app works fine without any auth, but when I try to add 
> basic authentication, it starts to produce 502 Bad Gateway error. Looked 
> into GAE logs, found error: 
>
> > from flask_httpauth import HTTPBasicAuth
> ModuleNotFoundError: No module named 'flask_httpauth'
>
> Ok, the module might be not installed yet, so: 
>
> > $ pip3 install flask_httpauth
> Requirement already satisfied: flask_httpauth in 
> ./.local/lib/python3.7/site-packages (4.2.0)
> Requirement already satisfied: Flask in 
> /usr/local/lib/python3.7/dist-packages (from flask_httpauth) (1.1.2)
> Requirement already satisfied: click>=5.1 in 
> /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (7.0)
> Requirement already satisfied: Werkzeug>=0.15 in 
> /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (1.0.1)
> Requirement already satisfied: itsdangerous>=0.24 in 
> /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (1.1.0)
> Requirement already satisfied: Jinja2>=2.10.1 in 
> /usr/local/lib/python3.7/dist-packages (from Flask->flask_httpauth) (2.11.2)
> Requirement already satisfied: MarkupSafe>=0.23 in 
> /usr/local/lib/python3.7/dist-packages (from 
> Jinja2>=2.10.1->Flask->flask_httpauth) (1.1.1)
>
> As you can see, it was already installed before. 
>
> Just in case, installed it for pip (not only for pip3)
>
> > $ pip install flask_httpauth
>
> After that I tried to call my web app again 
>
> https://example.com?who=test1234567
>
> but it produces the same error as above. 
>
> My main.py code: https://pastebin.com/CNzmgQ5c
> (also its code is below for your convenience)
>
> Please advise. 
>
>
> import flask
> from flask import Flask
> from flask import jsonify
> from Flask_HTTPAuth import HTTPBasicAuth
> app = flask.Flask(__name__)
> auth = HTTPBasicAuth()
> @app.route("/", methods=["GET"])
> def hello():
>         who = flask.request.args.get("who", "World")
>         return f"Hello {who}!\n"
> @app.route('/rest-auth')
> @auth.login_required
> def get_response():
>         return jsonify('You are authorized to see this message')
> @auth.verify_password
> def authenticate(username, password):
>         if username and password:
>                 if username == 'test' and password == '2345':
>                         return True
>                 else:
>                         return False
>         return False
> if __name__ == "__main__":
>     app.run(host="localhost", port=8080, debug=True)
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/3b7bad71-c264-4545-909b-5df78fb6369dn%40googlegroups.com.

Reply via email to