Permalink
Browse files

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.
  • Loading branch information...
1 parent 081ab87 commit 61c0a23cb181f53bc2b5670b9cad8e0ec009d06b @joeworkman committed Feb 9, 2012
Showing with 40 additions and 25 deletions.
  1. +24 −11 README.md
  2. +15 −13 github_oauth.php
  3. +1 −1 index.php
View
@@ -1,23 +1,31 @@
# GitHubOAuth PHP Library
-
+
+
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!
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.
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!?!
-
-## Requirements
+
+
+
+## Requirements
+
+
* PHP5.1+
* cURL Extension
-## Usage
+## Usage
+
+
First you need to load the class…
require_once('github_oauth.php');
-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/).
+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/).
+
- $oauth = new GitHubOauth($scope, $client_key, $secret_key);
+ $oauth = new GitHubOauth($scope, $client_key, $secret_key, $token_file);
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.
@@ -30,14 +38,18 @@ The next block of code is where all the magic happens. This code will load a loc
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 "/"**.
- $issues = json_decode($oauth->request("issues"));
+ $issues = $oauth->request("issues");
+
+
+
+
+## Functions
-
-## Functions
This class has eight public functions for you to use:
-
+
+
`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.
@@ -63,7 +75,8 @@ This class has eight public functions for you to use:
## Examples
-
+
+
The `index.php` file included in this repo can serve as a simple example on how to get going.
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.
View
@@ -1,7 +1,7 @@
<?php
-/* GitHubOauth Library v1.0 (http://github.com/joeworkman/github_oauth)
+/* GitHubOauth Library v1.1 (http://github.com/joeworkman/github_oauth)
* Developed by Joe Workman (http://joeworkman.net)
- * Copyright (c) 2011, Joe Workman. All rights reserved.
+ * Copyright (c) 2011-2012, Joe Workman. All rights reserved.
* Requires the PHP cURL extension
*/
class GitHubOauth {
@@ -19,10 +19,10 @@ class GitHubOauth {
private $redirect_uri;
private $curl;
- public function __construct($scope, $key, $secret) {
+ public function __construct($scope, $key, $secret,$token_file = './github_token') {
$this->scope = $scope;
$this->set_keys($key, $secret);
-
+ $this->token_file = $token_file;
$this->redirect_uri = $this->get_current_url();
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
@@ -44,7 +44,6 @@ public function revoke_access_token() {
unlink($this->token_file);
return;
}
-
// Redirect to GitHub and ask for user authorization.
// 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() {
public function request($request, $query = array()) {
$query['access_token'] = $this->access_token;
$query['per_page'] = $this->per_page;
- $url = 'https://api.github.com/'.$request . '?' . http_build_query($query);
-
- return $this->http_get($url);
+ $url = 'https://api.github.com/'.$request;
+ if (!empty($query)) {
+ $url .= '?' . http_build_query($query);
+ }
+ return json_decode($this->http_get($url));
}
public function post_request($request, $data = array(), $query = array()) {
$query['access_token'] = $this->access_token;
$query['per_page'] = $this->per_page;
- $url = 'https://api.github.com/'.$request . '?' . http_build_query($query);
-
- return $this->http_post($url, $data);
+ $url = 'https://api.github.com/'.$request;
+ if (!empty($query)) {
+ $url .= '?' . http_build_query($query);
+ }
+ return json_decode($this->http_post($url, json_encode($data)));
}
// Get the current page url
@@ -142,8 +145,7 @@ private function http_get($url) {
private function http_post($url, $data = array()) {
curl_setopt($this->curl, CURLOPT_POST, TRUE);
curl_setopt($this->curl, CURLOPT_POST, count($data));
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
-
+ curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
return $this->_request($url);
}
View
@@ -13,7 +13,7 @@
($oauth->has_access_token() or isset($_GET['code'])) ? $oauth->get_access_token() : $oauth->get_user_authorization();
// Submit your request. See http://developer.github.com/v3 for more details on possible queries
-$issues = json_decode($oauth->request("issues"));
+$issues = $oauth->request("issues");
?>
<html>

0 comments on commit 61c0a23

Please sign in to comment.