Skip to content

Commit

Permalink
Merge pull request #756 from trilliput/issue-755-fetch-email-when-sig…
Browse files Browse the repository at this point in the history
…n-up-with-github

issue #755: added fetching email functionality to github backend.
  • Loading branch information
omab committed Aug 29, 2013
2 parents 8bbaff0 + 1ac3674 commit 5b656f8
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions social_auth/backends/contrib/github.py
Expand Up @@ -46,11 +46,30 @@ class GithubBackend(OAuthBackend):
('expires', 'expires')
]

def _fetch_emails(self, access_token):
"""Fetch private emails from Github account"""
url = GITHUB_USER_DATA_URL + '/emails?' + urlencode({
'access_token': access_token
})

try:
data = simplejson.load(dsa_urlopen(url))
except (ValueError, HTTPError):
data = []
return data

def get_user_details(self, response):
"""Return user details from Github account"""
name = response.get('name') or ''
details = {'username': response.get('login'),
'email': response.get('email') or ''}
details = {'username': response.get('login')}

try:
email = self._fetch_emails(response.get('access_token'))[0]
except IndexError:
details['email'] = ''
else:
details['email'] = email

try:
# GitHub doesn't separate first and last names. Let's try.
first_name, last_name = name.split(' ', 1)
Expand Down

0 comments on commit 5b656f8

Please sign in to comment.