Home  >  Article  >  Backend Development  >  How does the AWS CLI open a browser and wait for a response before continuing?

How does the AWS CLI open a browser and wait for a response before continuing?

PHPz
PHPzforward
2024-02-10 21:30:10634browse

AWS CLI 如何打开浏览器并等待响应然后再继续?

php editor Banana will introduce to you how to use the AWS CLI command line tool to open the browser and wait for the response before continuing to perform other operations. AWS CLI is a command line tool provided by Amazon for managing AWS cloud services. It can perform various AWS operations through the command line interface. In some cases, we may need to open a browser in the command line and wait for the user to complete certain actions before continuing with subsequent commands. This article will introduce in detail how to use AWS CLI to implement this function, making your command line operations more flexible and convenient.

Question content

I'm trying to build a golang cli tool for my company and build login and some other functionality as part of the tool. I can't for the life of me figure out how AWS is able to open a browser window and wait for a few button clicks before continuing from the CLI.

https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_StartDeviceAuthorization.html

This is the CLI command I entered

aws sso login --profile login                                                                                                    

Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.us-east-1.amazonaws.com/

Then enter the code:

abcd-efgh
Successfully logged into Start URL: https://d-1421421423.awsapps.com/start

There is also Python documentation for starting device authentication and creating tokens

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sso-oidc/client/start_device_authorization.html https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sso-oidc/client/create_token.html

Solution

I just put it together One option that seems to work is a loop that checks every second

for attempts <= 30 {
            fmt.Println(attempts)
            token, err := idc.CreateToken(context.TODO(), &createTokenInput)
            if err != nil {
                // if debug is enabled show error
                log.Debug(err.Error())
                attempts++
                // wait 1 second
                time.Sleep(1 * time.Second)
            } else {
                response = *token
                break
            }
        }

edit:

After running AWS sso login --debug I noticed that the logs were actually looping and running the createToken query over and over again, so AWS was doing something similar to the above.

The above is the detailed content of How does the AWS CLI open a browser and wait for a response before continuing?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete