Bitbucket "Invalid credentials" 错误的解决方法
问题描述
当尝试向 Bitbucket 推送代码时,即使密码正确,也可能遇到以下错误:
bash
fatal: Invalid credentials
Password for 'https://username@bitbucket.org':
remote: Bitbucket Cloud recently stopped supporting account passwords for Git authentication.
remote: See our community post for more details: https://atlassian.community/t5/x/x/ba-p/1948231
remote: App passwords are recommended for most use cases and can be created in your Personal settings:
remote: https://bitbucket.org/account/settings/app-passwords/
fatal: Authentication failed for 'https://bitbucket.org/username/demo.git/'
认证方式变更
自 2022 年 3 月 1 日起,Bitbucket 不再支持使用 Atlassian 账户密码进行 Git HTTPS 认证,必须使用应用密码或 SSH 密钥。
解决方案
方法一:使用应用密码(推荐)
这是最常用的解决方法,适用于大多数情况。
创建应用密码步骤
- 访问 Bitbucket 应用密码设置页面
- 点击 "创建应用密码" (Create app password)
- 设置密码标签(用于识别)
- 选择适当的权限(至少需要仓库的读和写权限)
- 点击 "创建" 并复制生成的密码
重要提示
应用密码只会显示一次,请务必立即保存到安全的地方,关闭页面后将无法再次查看。
使用应用密码
bash
git remote set-url origin https://<Bitbucket用户名>:<应用密码>@bitbucket.org/<用户名>/<仓库名>.git
bash
git clone https://<Bitbucket用户名>:<应用密码>@bitbucket.org/<用户名>/<仓库名>.git
bash
# 添加通用凭据
网络地址: git:https://bitbucket.org
用户名: 你的Bitbucket用户名
密码: 应用密码
方法二:配置开发工具
JetBrains IDE (IntelliJ, Android Studio)
- 当 IDE 要求输入密码时,使用应用密码而非账户密码
- 或者通过设置 → 版本控制 → Git 重新配置认证
Visual Studio Code
- 安装 Atlassian 扩展
- 点击 Git 操作按钮
- 在授权对话框中使用应用密码
方法三:使用 SSH 密钥(推荐用于长期使用)
SSH 密钥认证更为安全且无需频繁输入密码。
SSH 密钥配置步骤
- 生成 SSH 密钥:
ssh-keygen -t ed25519 -C "your_email@example.com"
- 将公钥 (~/.ssh/id_ed25519.pub) 添加到 Bitbucket SSH 密钥设置
- 将远程仓库 URL 改为 SSH 格式:
git@bitbucket.org:用户名/仓库名.git
方法四:其他注意事项
- 确保使用用户名而非邮箱:认证时应使用 Bitbucket 用户名,而非登录邮箱
- 检查权限:确保应用密码具有足够的仓库访问权限
- 更新凭据管理器:Windows 用户需在凭据管理器中更新密码
问题原因
Bitbucket 为提高安全性,已停止支持使用账户密码进行 Git HTTPS 操作。这是 Atlassian 公司的全局安全策略变更,影响了所有使用 HTTPS 方式访问 Bitbucket 仓库的用户。
总结
解决 "Invalid credentials" 错误的关键是使用应用密码替代账户密码。对于长期开发者,建议配置 SSH 密钥以获得更安全和便捷的体验。
最佳实践
- 为不同的设备和应用创建独立的应用密码
- 定期轮换(更新)应用密码
- 优先使用 SSH 密钥进行认证
- 妥善保管应用密码,避免泄露
如果按照上述步骤仍无法解决问题,请检查网络连接或联系 Bitbucket 支持获取进一步帮助。