methods mentioned here are not secure as passwords must be stored in plain-text. for careless people only:
adding auth info to ~/.netrc works well (create if not exist)
machine github.com
login yourusername
password yourpassword
machine api.github.com
login yourusername
password yourpassword
make the file only readable/writeable from yourself (file owner)
chmod 600 ~/.netrc
~/.hgrc works well (create if not exist)
[ui]
username = yourusername <your@e.mail>
editor = vim
[auth]
bb.prefix = https://bitbucket.org
bb.username = yourusername
bb.password = yourpassword
make the file only readable/writeable from yourself (file owner)
chmod 600 ~/.hgrc
note: the ui section is not needed it is just relevant. also bb is not a special name; it can be anything, and there can be more than one set of auth information for other domains and accounts (grouped under their own arbitrary name)