Twitter and Facebook provide a public API to retrieve a user’s avatar by knowing only the username of the user. Recently Facebook changed its privacy settings and it is available only for pages, not for users.
For Twitter, given the user’s username (e.g. codingstill) the URL for the avatar is https://twitter.com/codingstill/profile_image. This URL gives a really small image (48×48) so if you need a bigger image append the
?image=original. For example: https://twitter.com/codingstill/profile_image?size=original.
For Facebook, we will use the public Graph API. For example if a page’s username is FacebookDevelopers, the avatar can be retrieved from https://graph.facebook.com/FacebookDevelopers/picture. That URL supports a few options; one of them is size, so you can ask for a big image by adding the
?type=large, for example https://graph.facebook.com/FacebookDevelopers/picture?type=large.