Created
March 24, 2011 12:27
-
-
Save indeyets/884968 to your computer and use it in GitHub Desktop.
GitHub OAuth + API test
This file contains hidden or 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
| <?php | |
| class GitHubAuth | |
| { | |
| const AUTH_URL = 'https://github.com/login/oauth/authorize'; | |
| const ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'; | |
| private $app_id; | |
| private $app_secret; | |
| public function __construct($app_id, $app_secret) | |
| { | |
| $this->app_id = $app_id; | |
| $this->app_secret = $app_secret; | |
| $this->redirect_uri = $redirect_uri; | |
| } | |
| public function authorize($redirect_uri) | |
| { | |
| $url = self::AUTH_URL.'?client_id='.urlencode($this->app_id).'&redirect_uri='.urlencode($redirect_uri); | |
| $this->redirectTo($url); | |
| } | |
| public function exchange_access_token($code, $redirect_uri) | |
| { | |
| $url = self::ACCESS_TOKEN_URL. | |
| '?client_id='.urlencode($this->app_id). | |
| '&redirect_uri='.urlencode($redirect_uri). | |
| '&client_secret='.urlencode($this->app_secret). | |
| '&code='.urlencode($code); | |
| $response = file_get_contents($url); | |
| $pieces = explode('=', $response, 2); | |
| return $pieces[1]; | |
| } | |
| } | |
| class GitHub | |
| { | |
| const API_URL = 'https://github.com/api/v2/json'; | |
| private $access_token; | |
| public function __construct($access_token) | |
| { | |
| $this->access_token = $access_token; | |
| } | |
| public function userInfo($user = null) | |
| { | |
| $url = self::API_URL.'/user/show'.(null === $user ? '' : '/'.$user).'?access_token='.urlencode($this->access_token); | |
| $response = file_get_contents($url); | |
| $data = json_decode($response); | |
| return $data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment