Created
June 29, 2014 07:05
-
-
Save huksley/aed541adcd074b242c05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/GitHubStrategy.php b/GitHubStrategy.php | |
index cae9dd2..812e1c9 100644 | |
--- a/GitHubStrategy.php | |
+++ b/GitHubStrategy.php | |
@@ -74,6 +74,17 @@ class GitHubStrategy extends OpauthStrategy { | |
if (!empty($results) && !empty($results['access_token'])) { | |
$user = $this->user($results['access_token']); | |
+ if (!$user['email']) { | |
+ // Read all emails and store primary | |
+ $emails = $this->userEmails($results['access_token']); | |
+ for ($i = 0; $i < count($emails); $i++) { | |
+ if ($emails[$i]['primary'] > 0) { | |
+ $user['email'] = $emails[$i]['email']; | |
+ break; | |
+ } | |
+ } | |
+ } | |
+ | |
$this->auth = array( | |
'uid' => $user['id'], | |
'info' => array(), | |
@@ -125,7 +136,15 @@ class GitHubStrategy extends OpauthStrategy { | |
* @return array Parsed JSON results | |
*/ | |
private function user($access_token) { | |
- $user = $this->serverGet('https://api.github.com/user', array('access_token' => $access_token), null, $headers); | |
+ $user = $this->serverGet('https://api.github.com/user', array(), | |
+ array( | |
+ 'http' => array( | |
+ 'header' => "Authorization: token $access_token\r\n" . | |
+ /** must set useragent or GitHub api will say Forbidden */ | |
+ "User-Agent: Mozilla\r\n" | |
+ ) | |
+ ), | |
+ $headers); | |
if (!empty($user)) { | |
return $this->recursiveGetObjectVars(json_decode($user)); | |
@@ -133,7 +152,7 @@ class GitHubStrategy extends OpauthStrategy { | |
else { | |
$error = array( | |
'code' => 'userinfo_error', | |
- 'message' => 'Failed when attempting to query GitHub v3 API for user information', | |
+ 'message' => 'Failed when attempting to query GitHub v3 API for user information (access_token = ' . $access_token . ')', | |
'raw' => array( | |
'response' => $user, | |
'headers' => $headers | |
@@ -143,4 +162,30 @@ class GitHubStrategy extends OpauthStrategy { | |
$this->errorCallback($error); | |
} | |
} | |
-} | |
\ No newline at end of file | |
+ | |
+ | |
+ /** | |
+ * Queries GitHub v3 API for emails | |
+ * | |
+ * @param string $access_token | |
+ * @return array Parsed JSON results | |
+ */ | |
+ private function userEmails($access_token) { | |
+ $emails = $this->serverGet('https://api.github.com/user/emails', array(), | |
+ array( | |
+ 'http' => array( | |
+ 'header' => "Authorization: token $access_token\r\n" . | |
+ /** must set useragent or GitHub api will say Forbidden */ | |
+ "User-Agent: Mozilla\r\n" | |
+ ) | |
+ ), | |
+ $headers); | |
+ | |
+ if (!empty($emails)) { | |
+ return $this->recursiveGetObjectVars(json_decode($emails)); | |
+ } else { | |
+ // Fail silentlty | |
+ return array(); | |
+ } | |
+ } | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment