oauth2 in GAE with python - python

I want to use a library which it use oauth2. Unfortunatelly, Google app engine cannot find oauth2 as a library.
When I browse my project I have the following error
import oauth2 as oauth
ImportError: No module named oauth2
I cannot use the oauth of google app engine, because I have to change the whole library.
Any advice how I can make oauth2 works on GAE?
The library use this version of oauth2 enter link description here

Ok you have to install the package first. It will install to your default python library. There are 3 ways to do this:
First way (MAC):
sudo easy_install oauth2
Second Way:
pip install oauth2
Third way (If you want to have more control over install location):
Download the zip file from here https://github.com/simplegeo/python-oauth2/archive/master.zip
Then unzip the file and navigate to the folder where it downloaded to.
Run
python3 setup.py
or
python setup.py
Depending on which package you'd like (python3 or 2)

Related

Python at AWS Lambda: `requests` from botocore.vendored deprecated, but `requests` not available

I've got a Python script for an AWS Lambda function that does HTTP POST requests to another endpoint. Since Python's urllib2.request, https://docs.python.org/2/library/urllib2.html, can only handle data in the standard application/x-www-form-urlencoded format and I want to post JSON data, I used the Requests library, https://pypi.org/project/requests/2.7.0/.
That Requests library wasn't available at AWS Lambda in the Python runtime environment, so had to be imported via from botocore.vendored import requests. So far, so good.
Today, I get a deprecation warning on that:
DeprecationWarning: You are using the post() function from 'botocore.vendored.requests'.
This is not a public API in botocore and will be removed in the future.
Additionally, this version of requests is out of date. We recommend you install the
requests package, 'import requests' directly, and use the requests.post() function instead.
This was mentioned in this blog post from AWS too: https://aws.amazon.com/blogs/developer/removing-the-vendored-version-of-requests-from-botocore/.
Unfortunately, changing from botocore.vendored import requests into import requests results in the following error:
No module named 'requests'
Why is requests not available for the Python runtime at AWS Lambda? And how can I use / import it?
I succeeded sending HTTP POST requests using the urllib3 library, which is available at AWS Lambda without the requirements for additional installation instructions.
import urllib3
http = urllib3.PoolManager()
response = http.request('POST',
url,
body = json.dumps(some_data_structure),
headers = {'Content-Type': 'application/json'},
retries = False)
Check out the instructions here: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html#python-package-dependencies
All you need to do is download the requests module locally, then include it in your Lambda function deployment package (ZIP archive).
Example (if all your Lambda function consisted of was a single Python module + requests module):
$ pip install --target ./package requests
$ cd package
$ zip -r9 ${OLDPWD}/function.zip .
$ cd $OLDPWD
$ zip -g function.zip lambda_function.py
$ aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip
Answer 2020-06-18
I found a nice and easy way to use requests inside AWS Lambda functions!
Open this link and find the region that your function is using:
https://github.com/keithrozario/Klayers/tree/master/deployments/python3.8/arns
Open the .csv related to your region and search for the requests row.
This is the ARN related to requests library:
arn:aws:lambda:us-east-1:770693421928:layer:Klayers-python38-requests:6
So now in your lambda function, add a layer using the ARN found.
Obs.: make sure your Python lambda function runtime is python3.8.
If you are using serverless framework
Specify the plugin in serverless.yml
plugins:
- serverless-python-requirements
At the directory root create file requirements.txt
requirements.txt
requests==2.22.0
This will install the requests and packages mentioned.
requests is NOT part of core python.
See https://docs.aws.amazon.com/en_pv/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html about packaging a Lambda having external dependencies (in your case the requests library)
Amazon's Serverless Application Model (SAM) provides a build command that can bundle arbitrary python dependencies into the deployment artifact.
To be able to use the requests package in your code, add the dependency to your requirements.txt file:
requests==2.22.0
then run sam build to get an artifact that vendors requests. By default, your artifacts will be saved to the .aws-sam/build directory but another destination directory can be specified with the --build-dir option.
Consult SAM's documentation for more info.
Here's my redneck solution that works with any library, using an AWS Lambda Layer:
This has the advantage that you don't have to trust any 3rd party layers, because you can easily make it yourself.
Go to your local python's Lib/site-packages (python install location or your venv)
Copy whichever libraries you need (e.g. "requests") into a folder named "python"
Zip this folder
Create an AWS Lambda Layer, and upload the zip into it
Add this layer in your lambda function
Import your libraries as usual, and keep coding as if nothing happened
pip install requests
and then
import requests
to use.

Azure App Service ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

On App Azure Linux with Python, the Mysql module seem not work :
2018-12-24T19:11:38.215760010Z import _mysql
2018-12-24T19:11:38.215763810Z ImportError: libmysqlclient.so.18: cannot
open shared object file: No such file or directory
...
2018-12-24T19:11:27.536810347Z django.core.exceptions.ImproperlyConfigured:
Error loading MySQLdb module.
2018-12-24T19:11:27.536813747Z Did you install mysqlclient?
requirement :
django
mysqlclient
Has anyone ever managed to run django on azure web app?
This is a common error. Using mysqlclient also requires native dependencies to be installed: either the mysql client or the mysql-compatible mariadb client. In order to address these issues the easiest way, change your project to use mysql-connector-python instead of mysqlclient. You will also have to update your settings so that any database engine that uses django.db.backends.mysql should be updated to mysql.connector.django.
It sounds like there is not mysql native client library installed in your Azure App for Linux.
Here is two cases for building custom image.
For Debian or Ubuntu image, please run apt install libmysqlclient-dev firstly to preinstall libmysqlclient.so on your Docker image.
For Fedora or CentOS iamge, please run yum install mysql-libs firstly to preinstall the same one.
Or you can directly use the existing image which has preinstalled these required libs from Azure Container Registry or DockerHub.
Please take a try that go to the app service scm site, and find the pip location, then use pip to install the required module.

Python flask saml throwing saml2.sigver.SigverError Error Message

Has anyone succesfully implemented flask-saml using Windows as dev environment, Python 3.6 and Flask 1.0.2?
I was given the link to the SAML METADATA XML file by our organisation and had it configured on my flask app.
app.config.update({
'SECRET_KEY': 'changethiskeylaterthisisoursecretkey',
'SAML_METADATA_URL': 'https://<url>/FederationMetadata.xml',
})
flask_saml.FlaskSAML(app)
According to the documentation this extension will setup the following routes:
/saml/logout/: Log out from the application. This is where users go
if they click on a “Logout” button.
/saml/sso/: Log in through SAML.
/saml/acs/: After /saml/sso/ has sent you to your IdP it sends you
back to this path. Also your IdP might provide direct login without
needing the /saml/sso/ route.
When I go to one of the routes http://localhost:5000/saml/sso/ I get the error below
saml2.sigver.SigverError saml2.sigver.SigverError: Cannot find
['xmlsec.exe', 'xmlsec1.exe']
I then went to this site https://github.com/mehcode/python-xmlsec/releases/tag/1.3.5 to get xmlsec and install it. However, I'm still getting the same issue.
Here is a screenshot of how I installed xmlsec
where does not seem to find the xmlsec.exe
documentationis asking to have xmlsec1 pre-installed. What you installed is a python binding to xmlsec1.
Get a windows build of xmlsec1 from here or build it from source
And make it available in the PATH.
xmlsec won't work properly in windows, better use Linux environment
Type the below command before giving pip install xmlsec
sudo apt-get install xmlsec1

How to 'pip install packages' inside Azure WebJob to resolve package compatibility issues

I am deploying a WebJob inside Azure Web App that uses Google Maps API and Azure SQL Storage.
I am following the typical approach where I make a WebJob directory and copy my 'site-packages' folder inside the root folder of the WebJob. Then I also add my code folder inside 'site-packages' and make a run.py file inside the root that looks like this:
import sys, os
sys.path.append(os.path.join(os.getcwd(), "site-packages"))
import aero2.AzureRoutine as aero2
aero2.run()
Now the code runs correctly in Azure. But I am seeing warnings after a few commands which slow down my code.
I have tried copying 'pyopenSSL' and 'requests' module into my site-packages folder, but the error persists.
However, the code runs perfectly on my local machine.
How can I find this 'pyopenSSL' or 'requests' that is compatible with the python running on Azure?
Or
How can I modify my code so that it pip installs the relevant packages for the python running on Azure?
Or more importantly,
How can I resolve this error?
#Saad,
If your webjob worked fine on Azure Web App, but you got inscuritywaring, I suggest you can try to disable the warning information via this configuration(https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings ).
Meanwhile,requests lib has some different with the high version, I recommend you refer to this document:
http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html
And Azure web app used the Python 2.7.8 version which is lower than 2.7.9. So you can download the requests lib as version 2.5.3
According the doc referred in the warning message https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning:
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
So the easiest way fix this warning, is to upgrade the python version of the Azure Web Apps. Login the Azure manager portal, change the python version to 3.4 in Application settings column:
As I test in webjob task to use requests module to request a "https://" url, and since upgrade python version to 3.4, there are no more warnings.
I followed this article and kind of 'pip installed' the pymongo library for my script. Not sure if it works for you but here are the steps:
Make sure you include the library name and version in the requirements.txt
Deploy the web app using Git. The directory should include at least
requirements.txt (only to install whatever is in requirements.txt in the virtual environment, which is shared with Web App in D:\home\site\wwwroot\env\Lib\site-packages)
add this block of code to the Python code you want to use in the WebJob zip file.
import sys
sitepackage = "D:\home\site\wwwroot\env\Lib\site-packages"
sys.path.append(sitepackage)

How to use "import oauth2" in Google App Engine?

I want to use Tumblr's API v2, which includes OAuth. so I need to find a OAuth module.
I am supposed to use it like this:
import oauth2 as oauth
I have found the oauth2 source code here: https://github.com/simplegeo/python-oauth2
If I want to use it in my Linux Ubuntu 10.08, whats the process? I have installed git. I notice that there is a "setup.py", so I just have to run it? The ReadME https://github.com/simplegeo/python-oauth2/blob/master/README.md doesnt mention how to install, maybe it is too newbie.
If I want to use it in Google App Engine, how can I do it?
Thanks for your help. I am pretty new to GIT stuff.
On Ubuntu, simply sudo apt-get install python-oauth2 and the package will be installed for you automatically.
For AppEngine, you might take a look at the Google API Python Client's appengine examples. They have an OAuth2 client specifically designed to work with AppEngine.

Categories