Browse files

Initial commit

  • Loading branch information...
1 parent 8971b96 commit 163e50ee766054710425be525c74b1c15d181c76 @vjeux committed Mar 6, 2012
Showing with 93 additions and 0 deletions.
  1. +6 −0 login.html
  2. +72 −0 main.html
  3. +15 −0 token.php
View
6 login.html
@@ -0,0 +1,6 @@
+<script>
+// Step 3
+var code = window.location.toString().replace(/.+code=/, '');
+window.opener.postMessage(code, window.location);
+window.close();
+</script>
View
72 main.html
@@ -0,0 +1,72 @@
+<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
+
+<style>
+label, textarea, a { display: block; }
+hr { margin: 20px 0; }
+input { width: 300px; }
+</style>
+
+<h2>Login</h2>
+<button id="login">Login</button>
+<label>code <input type="text" id="code"></input></label>
+<label>access_token <input type="text" id="access_token"></input></label>
+<label>username <input type="text" id="username"></input></label>
+
+<script>
+// Step 2
+$('#login').click(function () {
+ window.open('https://github.com' +
+ '/login/oauth/authorize' +
+ '?client_id=58a3dcf21a0bae21db44' +
+ '&scope=gist');
+});
+
+// Step 4
+window.addEventListener('message', function (event) {
+ var code = event.data;
+ $('#code').val(code);
+
+ // Step 5
+ $.get('token.php?code=' + code, function (access_token) {
+ // Step 7
+ $('#access_token').val(access_token);
+
+ $.getJSON('https://api.github.com/user?access_token=' + access_token, function (user) {
+ $('#username').val(user.login);
+ });
+ });
+});
+</script>
+
+<hr />
+
+<h2>Interact</h2>
+<textarea id="gist_content">Let's make a gist!</textarea>
+<button id="gist_new">New private gist</button>
+<label>link <input type="text" id="gist_link"></input></label>
+
+<script>
+$('#gist_new').click(function () {
+ var url = 'https://api.github.com/gists' +
+ '?access_token=' + $('#access_token').val();
+
+ var data = JSON.stringify({
+ "public": false,
+ "files": {
+ "sample.txt": {
+ "content": $('#gist_content').val()
+ }
+ }
+ });
+
+ $.post(url, data, function (gist) {
+ $('#gist_link').val(gist.html_url);
+ });
+});
+</script>
+
+<hr />
+
+<h2>Useful links</h2>
+ <a href="https://github.com/account/applications/" target="_blank">(Dev) Create Github application</a>
+ <a href="https://github.com/settings/applications/" target="_blank">(User) Manage Github application accesses</a>
View
15 token.php
@@ -0,0 +1,15 @@
+<?php
+// Step 6
+$data = 'client_id=' . '58a3dcf21a0bae21db44' . '&' .
+ 'client_secret=' . 'd102461f3339bad28ac26998be39a1e26b5205b9' . '&' .
+ 'code=' . urlencode($_GET['code']);
+
+$ch = curl_init('https://github.com/login/oauth/access_token');
+curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+$response = curl_exec($ch);
+
+preg_match('/access_token=([0-9a-f]+)/', $response, $out);
+echo $out[1];
+curl_close($ch);
+?>

0 comments on commit 163e50e

Please sign in to comment.