如何通过Shell脚本在Cloud9 IDE中登录Heroku?
Got it, let's sort out this automated Heroku login problem for you. The issue with your current setup is that heroku login defaults to trying to open a browser for OAuth authentication, so it doesn't actually read the email/password you're piping in via stdin. Here are two reliable ways to make this work:
Option 1: Force Interactive Mode for Pipe Input
First, update your login.sh script to use the interactive flag—this forces the CLI to prompt for credentials directly in the terminal, which lets it read the input from your pipe:
# Inside login.sh heroku login --interactive
Then run your script with the email and password piped in like this:
echo -e "valid@email.com\nyour-heroku-password\n" | ./login.sh
Option 2: Use API Key Authentication (Recommended)
This method is more secure and stable, especially if you have 2FA enabled on your Heroku account (since password-based login won't work with 2FA). Here's how to set it up:
- Get your Heroku API token: If you're already logged in somewhere, run this command to grab your token:
heroku auth:token
You can also find this token in your Heroku Account Settings under the "API Key" section.
- Update your login script: Instead of using password login, pipe the API token to
heroku auth:login:
# Inside login.sh echo "your-api-token-goes-here" | heroku auth:login
Or even cleaner, use an environment variable to avoid hardcoding the token (you can add the export line to ~/.bashrc in Cloud9 to persist it):
# First set the env var export HEROKU_API_KEY="your-api-token-goes-here" # Then in login.sh echo "$HEROKU_API_KEY" | heroku auth:login
Important Notes
- Never hardcode your password or API token directly in your script! Using environment variables keeps sensitive info secure and easy to update.
- If you have two-factor authentication enabled on your Heroku account, password-based login (Option 1) will fail—stick with the API key method (Option 2) in that case.
内容的提问来源于stack exchange,提问作者kofhearts

