Generate JWT in Google cloud function - python

陌路散爱 提交于 2021-02-11 09:42:17

问题


I wrote a script and uploaded it to GCFunctions. Now, one of my functions is using PyJWT library in order to generate JWT for GCP API calls, the problem is that I keep getting errors every time I run the function. When I added pyjwt to 'requirements.txt' I got the error: 'Algorithm RS256 could not be found', then I tried to add cryptography (the encrypting library that pyjwt uses), and also tried pycrypto (for RS256 registering) but still nothing. I'd be grateful for some help here! even suggestions for other ways of authentication methods in GCP API calls would be great! Thanks in advance!!

Edit: BTW- the function is running on Python3.7

Here is the content of my requirements.txt file (dependencies)

# Function dependencies, for example:
# package>=version
requests==2.21.0
pycrypto
pyjwt==1.7.1
pyjwt[crypto]
boto3==1.11.13

And this is the exception I get while trying to add pyjwt[crypto] and run the script once again: enter image description here


回答1:


Found a way to make it work. Posting it here for those who will face it in the future... I decided eventually to upload a zip file that contains the code file + requirements.txt + service account JSON Credentials file and added the following libraries as dependencies(to requirements.txt): oauth2client, google-api-python-client.

Here's how I did it:

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
import logging

# set the service with the credentials
credentials = GoogleCredentials.from_stream("my_creds.json")
service = discovery.build('compute', 'v1', credentials=credentials)
# block errors printing for 'googleapicliet.discovery'
logging.getLogger('googleapicliet.discovery_cache').setLevel(logging.ERROR)

def main(event, context):
    # Project ID for this request.
    project = '<project_id>'  

    # The name of the zone for this request.
    zone = '<zone>'

    # Name of the instance resource to return.
    instance = '<instance-id>'

    request = service.instances().get(project=project, zone=zone, instance=instance)
    response = request.execute()

    # print only network details of the instance
    print("'{}' Network Details: {}".format(response['name'], response['networkInterfaces'][0]['accessConfigs'][0]))



回答2:


As specified in the PyJWT installation documentation, if you plan to do encoding or decoding of JWTs you should use the following pip install command to install it as a required extra along with pyjwt:

pip install pyjwt[crypto]

or add pyjwt[crypto] as a new line in your requirements.txt.




回答3:


As you want to use the GCP APIs I would recommend you to use the client libraries, as when using the client libraries the authentication is managed by the library itself, therfor there is no need of managing the security by yourself.

here It is recommended by Google to avoid doing the authorization process explecitely. and instead using the propper client libraries.



来源:https://stackoverflow.com/questions/60534460/generate-jwt-in-google-cloud-function-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!