简体   繁体   中英

How to pass received (bearer) access token to generated REST Client in order to invoke secured API-Gateway Endpoint

Im fairly new to AWS and its Cognito and API-Gateway services. I have created in AWS a Cognito-specific User Pool and an AWS-specific API-Gateway API with some API-Endpoints to be accessed via REST API calls. The API-Gateway "Authorizer" is set to "Cognito". After that, I have exported the Swagger document/OpenAPI2.0 using the AWS-Console specific export function and generated with the Swagger Editor a Python REST Client API.

The generated REST Client SDK generated the Model-specific "GET" function, eg:

# create an instance of the API class
api_instance = swagger_client.DefaultApi()
user_id = 'user_id_example' # str | 

try:
    api_response = api_instance.user_get(user_id)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling DefaultApi->user_get: %s\n" % e)

In order to get a correct result from the function call api_instance.user_get(user_id) I need somehow pass the access token to that function.

The question is now, how do I pass the access token - which I have successfully obtained after the User signed-in - to the Python REST Client API in order to invoke an API-Endpoint function which has an "Cognito" authorizer set?

I saw many expamples how to realize this with Postman or CURL, but this is not what I'm looking for. I want to invoke my "Cognito" protected API-Endpoint in AWS API-Gateway with the generated REST API Client. I assume, there must be a way to put the received access token to the "Authorization" Header in the HTTP-Request call, before the generated REST Client function is invoked.

Any help is very appreciated.

I'm not sure if I've understood you correctly, but this might help you.

import requests
endpoint = ".../api/ip"
data = {"ip": "1.1.2.3"}
headers = {"Authorization": "Bearer MyBearerAuthTokenHere"}

print(requests.post(endpoint, data=data, headers=headers).json())

#Edit You don't need to parse the response as json if it isn't. This is just an Sample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM