Skip to content

Commit 61c0a23

Browse files
committed
Upgraded to v1.1
The library now does all the json encoding and decoding for you. There is a new $token_file argument when you initiate the class. This will be used as the full path to the file where the github oauth token will be stored for future use. Fixed the post_request method so that it now properly encodes and passes the data through.
1 parent 081ab87 commit 61c0a23

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
# GitHubOAuth PHP Library
2-
2+
3+
34
This library provides a very simple PHP interface to the GitHub oAuth API v3. Just a few lines of code and you are ready to go!
45

56
You need to [register the application](https://github.com/account/applications/new) that you intend to use this library with GitHub. The OAuth keys provided will only work for the one domain that registered. Therefore, if you have multiple domains, you that you would like to run this library on, you will be required to register each domain and obtain a unique set of OAuth keys for each domain.
67

78
I ultimately developed this library becuase I wanted to give access to my github issues of my private repositories to my customers. This way my customers can see what bugs exist and see what I am working on. Take a look at my [Known Issues & Requests](http://joeworkman.net/rapidweaver/issues) page on my website that displays all issues for on of my private repos. you can even drill down into each individual issue and see all of its comments. While all of this is read-only, you could easily add modification and creation abilities using this library. Pretty cool huh!?!
8-
9-
## Requirements
9+
10+
11+
12+
## Requirements
13+
14+
1015
* PHP5.1+
1116
* cURL Extension
1217

13-
## Usage
18+
## Usage
19+
20+
1421
First you need to load the class…
1522

1623
require_once('github_oauth.php');
1724

18-
Now we need to create our OAuth object. You will need to make sure that you obtain your OAuth Client ID and Secret key by [registering your app](https://github.com/account/applications/new) with the GitHub. You will also need to specify the scope of the access you are requesting. For more info on scope options, check out the [GitHub OAuth Docs](http://developer.github.com/v3/oauth/).
25+
Now we need to create our OAuth object. You will need to make sure that you obtain your OAuth Client ID and Secret key by [registering your app](https://github.com/account/applications/new) with the GitHub. You will also need to specify the scope of the access you are requesting. *$token_file* is an optional argument for defining a centralized location to store the oauth token for future use. For more info on scope options, check out the [GitHub OAuth Docs](http://developer.github.com/v3/oauth/).
26+
1927
20-
$oauth = new GitHubOauth($scope, $client_key, $secret_key);
28+
$oauth = new GitHubOauth($scope, $client_key, $secret_key, $token_file);
2129

2230
The next block of code is where all the magic happens. This code will load a local cached copy of your OAuth access\_token. If one is not found, it will redirect the user to GitHub for authorization. Once the user authorizes the access, you will be redirected back to the original page where the class will generate the access\_token based off the code supplied by GitHub.
2331

@@ -30,14 +38,18 @@ The next block of code is where all the magic happens. This code will load a loc
3038

3139
Now we are all set! We are fully authenticated and can make all the API queries that we want! **Make sure that your requests do not start with a "/"**.
3240

33-
$issues = json_decode($oauth->request("issues"));
41+
$issues = $oauth->request("issues");
42+
43+
44+
45+
46+
## Functions
3447

3548

36-
37-
## Functions
3849
This class has eight public functions for you to use:
3950

40-
51+
52+
4153
`set_keys($client_id, $secret)` allows you to set the oauth keys outside of the object creation. Chances are that you won't have to use this method.
4254

4355

@@ -63,7 +75,8 @@ This class has eight public functions for you to use:
6375

6476

6577
## Examples
66-
78+
79+
6780
The `index.php` file included in this repo can serve as a simple example on how to get going.
6881

6982
For a live example, take a look at my [Known Issues & Requests](http://joeworkman.net/rapidweaver/issues) page on my website that displays all issues for on of my private repos. you can even drill down into each individual issue and see all of its comments. While all of this is read-only, you could easily add modification and creation abilities using this library.

github_oauth.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
/* GitHubOauth Library v1.0 (http://github.com/joeworkman/github_oauth)
2+
/* GitHubOauth Library v1.1 (http://github.com/joeworkman/github_oauth)
33
* Developed by Joe Workman (http://joeworkman.net)
4-
* Copyright (c) 2011, Joe Workman. All rights reserved.
4+
* Copyright (c) 2011-2012, Joe Workman. All rights reserved.
55
* Requires the PHP cURL extension
66
*/
77
class GitHubOauth {
@@ -19,10 +19,10 @@ class GitHubOauth {
1919
private $redirect_uri;
2020
private $curl;
2121

22-
public function __construct($scope, $key, $secret) {
22+
public function __construct($scope, $key, $secret,$token_file = './github_token') {
2323
$this->scope = $scope;
2424
$this->set_keys($key, $secret);
25-
25+
$this->token_file = $token_file;
2626
$this->redirect_uri = $this->get_current_url();
2727
$this->curl = curl_init();
2828
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
@@ -44,7 +44,6 @@ public function revoke_access_token() {
4444
unlink($this->token_file);
4545
return;
4646
}
47-
4847

4948
// Redirect to GitHub and ask for user authorization.
5049
// GitHub should redirect page to the current page with our access code as a page parameter
@@ -95,16 +94,20 @@ public function get_access_token() {
9594
public function request($request, $query = array()) {
9695
$query['access_token'] = $this->access_token;
9796
$query['per_page'] = $this->per_page;
98-
$url = 'https://api.github.com/'.$request . '?' . http_build_query($query);
99-
100-
return $this->http_get($url);
97+
$url = 'https://api.github.com/'.$request;
98+
if (!empty($query)) {
99+
$url .= '?' . http_build_query($query);
100+
}
101+
return json_decode($this->http_get($url));
101102
}
102103
public function post_request($request, $data = array(), $query = array()) {
103104
$query['access_token'] = $this->access_token;
104105
$query['per_page'] = $this->per_page;
105-
$url = 'https://api.github.com/'.$request . '?' . http_build_query($query);
106-
107-
return $this->http_post($url, $data);
106+
$url = 'https://api.github.com/'.$request;
107+
if (!empty($query)) {
108+
$url .= '?' . http_build_query($query);
109+
}
110+
return json_decode($this->http_post($url, json_encode($data)));
108111
}
109112

110113
// Get the current page url
@@ -142,8 +145,7 @@ private function http_get($url) {
142145
private function http_post($url, $data = array()) {
143146
curl_setopt($this->curl, CURLOPT_POST, TRUE);
144147
curl_setopt($this->curl, CURLOPT_POST, count($data));
145-
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
146-
148+
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
147149
return $this->_request($url);
148150
}
149151

index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
($oauth->has_access_token() or isset($_GET['code'])) ? $oauth->get_access_token() : $oauth->get_user_authorization();
1414

1515
// Submit your request. See http://developer.github.com/v3 for more details on possible queries
16-
$issues = json_decode($oauth->request("issues"));
16+
$issues = $oauth->request("issues");
1717
?>
1818

1919
<html>

0 commit comments

Comments
 (0)