要在AWS CLI中打开浏览器并在继续之前等待响应,可以使用以下步骤和代码示例:
pip install boto3 selenium
open_browser.py
,并在其中添加以下代码:import boto3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 使用AWS CLI获取授权URL
def get_authorization_url():
sts_client = boto3.client('sts')
response = sts_client.get_caller_identity()
account_id = response['Account']
cognito_client = boto3.client('cognito-idp')
response = cognito_client.get_user_pool_client(
UserPoolId='your_user_pool_id',
ClientId='your_client_id'
)
client_id = response['UserPoolClient']['ClientId']
auth_url = f'https://your_domain.auth.us-west-2.amazoncognito.com/login?response_type=code&client_id={client_id}&redirect_uri=https%3A%2F%2Fyour_callback_url%2F{account_id}'
return auth_url
# 使用Selenium打开浏览器并等待响应
def open_browser_and_wait(auth_url):
# 创建Chrome浏览器实例
driver = webdriver.Chrome()
# 打开授权URL
driver.get(auth_url)
# 等待用户登录并授权
WebDriverWait(driver, 600).until(EC.presence_of_element_located((By.XPATH, '//*[@id="continue"]')))
# 等待用户完成操作并关闭浏览器
driver.quit()
# 主函数
def main():
auth_url = get_authorization_url()
open_browser_and_wait(auth_url)
if __name__ == '__main__':
main()
your_user_pool_id
:您的用户池IDyour_client_id
:您的用户池客户端IDyour_domain
:您的Cognito域名your_callback_url
:接收授权码的回调URLpython open_browser.py
这将使用AWS CLI获取授权URL并在浏览器中打开该URL。脚本将等待用户登录并授权,然后关闭浏览器。您可以根据需要调整等待时间。