在Google Cloud函数中生成JWT-python [英] Generate JWT in Google cloud function - python

查看:57
本文介绍了在Google Cloud函数中生成JWT-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本并将其上传到GCFunctions.现在,我的功能之一是使用 PyJWT库,以便为GCP API调用生成JWT,问题是,每次运行该函数时,都会不断出现错误.当我将pyjwt添加到"requirements.txt"时,出现错误:找不到算法RS256",然后我尝试添加加密技术(pyjwt使用的加密库),还尝试了pycrypto(用于RS256注册),但还是没有.在此感谢您的帮助!甚至有关GCP API调用中其他身份验证方法方式的建议也很棒!提前谢谢!

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!!

顺便说一句,该函数正在Python3.7上运行

BTW- the function is running on Python3.7

这是我的requirements.txt文件的内容(依赖项)

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

这是我尝试添加 pyjwt [crypto] 并再次运行脚本时遇到的异常:在此处输入图片描述

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

推荐答案

找到了使其工作的方法.将其发布在这里,供那些将来会遇到的人...我最终决定上传一个包含代码文件+ requirements.txt +服务帐户JSON凭据文件的zip文件,并添加以下库作为依赖项(至requirements.txt):oauth2client,google-api-python-client.

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.

这是我的做法:

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]))

这篇关于在Google Cloud函数中生成JWT-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆