简体   繁体   English

Python:如何使用请求库访问 Google App Engine 上托管的 Flask RestPlus API?

[英]Python: How to Use Requests Library to Access Flask RestPlus API hosted on Google App Engine?

I built a rest api using flask restplus and I tested it using the swagger documentation, and it seems to be fully functional and working just fine.我使用flask restplus 构建了一个rest api,并使用swagger 文档对其进行了测试,它似乎功能齐全并且工作正常。 I now want to test the API using the python requests library, but I'm getting the following error:我现在想使用 python requests 库测试 API,但出现以下错误:

Invalid IAP credentials: empty token

I imagine this is because I did not authorize using the credentials.我想这是因为我没有授权使用凭据。 I do have access to the service account credentials json file, but just wondering how I pass this into requests?我确实有权访问服务帐户凭据 json 文件,但只是想知道如何将其传递给请求?

Code Example:代码示例:

url = 'https://crosssellapi-project-id.appspot.com/auth/login'

r = requests.get(url, headers={'accept': 'application/json'})

Thanks in advance.提前致谢。

I think the best way to accomplish this is using the google-auth library for python.我认为最好的方法是使用 python 的google-auth库。

$ pip install google-auth

I suggest to create a service account, give it the role IAP-secured Web App User and then download the JSON key for that service account.我建议创建一个服务帐户,赋予它IAP-secured Web App User角色,然后下载该服务帐户的 JSON 密钥。

Go to API's & Services > Credentials and copy the Client ID of the OAuth 2.0 client you want to use for your API.转至API 和服务 > 凭据并复制要用于 API 的 OAuth 2.0 客户端的Client ID

Finally use this code snippet to make a request:最后使用此代码片段发出请求:

from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession

SERVICE_FILENAME = '/path/to/your/service_account_key.json'
API_URL = 'https://YOUR_API_URL'
AUDIENCE = 'YOUR_OAUTH_CLIENT_ID'

credentials = service_account.IDTokenCredentials.from_service_account_file(SERVICE_FILENAME, target_audience=AUDIENCE)

session = AuthorizedSession(credentials)

r = session.get(API_URL, headers={'accept': 'application/json'})

print(r.text)

If you want to know more about google-auth check this .如果您想了解有关google-auth更多信息,请查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Google App Engine 与 Flask 和 Flask-restplus 一起使用时出现 404 错误 - 404 error when using Google App Engine with flask and flask-restplus 如何在Flask Restplus中调用API? - How to call an API in Flask Restplus? 如何使用OAuth从Python应用程序访问Google App Engine端点API? - How can I access Google App Engine endpoints API from Python application with use OAuth? 如何在 Flask restplus Api 中使用基于方法参数的 SELECT 查询? - How to use SELECT query based on method parameter in Flask restplus Api? google app引擎python flask内置库 - google app engine python flask built-in library 如何访问用于在Python App Engine中存储数据的低级API - How to access lowlevel API for storing data in Google App Engine for python 可以在Google App Engine上使用Python Requests库吗? - Can Python Requests library be used on Google App Engine? 如何在Google App Engine上使用MathJax库 - How to use MathJax library on Google App Engine Google App Engine在Python / Flask API上产生502错误 - Google App Engine Produces 502 Error on Python/Flask API 发送图像数据到 Flask restplus API with Python - Sending image data to Flask restplus API with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM