-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
133 lines (117 loc) · 3.38 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
include ('./config.php');
spl_autoload_register(function ($class) {
$class = str_replace('\\', '/', $class);
if (file_exists('src/' . $class . '.php')) {
include 'src/' . $class . '.php';
}
if (file_exists('lib/' . $class . '.php')) {
include 'lib/' . $class . '.php';
}
});
$obj = new OAuth_io\OAuth();
$obj->initialize($config['key'], $config['secret']);
if (isset($_GET['state'])) {
echo $obj->generateStateToken();
} else if (isset($_GET['code'])) {
$req = $obj->auth('facebook', array(
"code" => $_GET['code']
));
$response = $req->get('/me');
echo $response['name'];
} else {
?>
<html>
<head>
<title>Retrieve my name</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Testing OAuth.io PHP SDK without Composer</h1>
<p>
This app demonstrates how to use OAuth.io without Composer, using a single
PHP script - it doesn't really deserve a best practices award, but it works ;)
</p>
<p>
Just remember to copy <code>config.sample.php</code> to <code>config.php</code> and setup your OAuth.io keys in it.
</p>
<h3>Retrieve my name from Facebook with OAuth.io's popup</h3>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="./oauth.js"></script>
<button class="state btn btn-success">Get my name from Facebook</button>
<div>
<strong>Result:</strong> <span class="myname_popup"></span>
</div>
<hr>
<h3>Retrieve my name from Facebook with OAuth.io's redirect</h2>
<button class="redirect_button btn btn-success">Redirect to Facebook to get my name</button>
<div>
<strong>Result:</strong> <span class="myname_redirect"></span>
</div>
</div>
<script>
$(document).ready(function () {
OAuth.initialize("<?php echo $config['key'] ?>");
$('.state').click(function () {
$.ajax({
url: '?state'
})
.done(function (result) {
console.log ('popuping with code', result)
OAuth.popup('facebook', {
state: result
})
.done(function (result) {
console.log(result);
$('.myname_popup').html("Loading your name, please wait");
$.ajax({
url: '?code=' + result.code
})
.done(function (r) {
$('.myname_popup').html(r);
})
.fail(function (e) {
console.log(e);
});
})
.fail(function (e) {
console.log(e);
});
})
});
// Redirect method
$('.redirect_button').click(function () {
$.ajax({
url: '?state'
})
.then(function (state) {
OAuth.redirect('facebook', { state: state }, "<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ?>");
})
});
var callback = OAuth.callback('facebook');
if (callback) {
callback
.then(function (result) {
$('.myname_redirect').html("Loading your name, please wait");
$.ajax({
url: '?code=' + result.code
})
.done(function (r) {
$('.myname_redirect').html(r);
})
.fail(function (e) {
console.log(e);
});
})
.fail(function (e) {
console.log(e);
});
}
});
</script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
<?php
} ?>