-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowdpanthers-oauth.php
More file actions
96 lines (77 loc) · 3.49 KB
/
crowdpanthers-oauth.php
File metadata and controls
96 lines (77 loc) · 3.49 KB
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
<?php
/*
/oauth2/authorize?client_id={{client_id}}&redirect_uri={{callback_url}}&response_type=code
/oauth2/token?grant_type=authorization_code&code={{code}}&redirect_uri={{callback_url}}&client_id={{client_id}}&client_secret={{client_secret}}
{{domain}}/oauth2/token/?grant_type=authorization_code&code={{auth_code}}&redirect_uri={{callbakc_uri}}&client_id={{client_id}}&client_secret={{client_secret}}
/oauth2/token?client_id={{client_id}}&client_secret={{client_secret}}&grant_type=client_credentials
*/
class crowdpanthersoAuth {
public $CurlHeaders;
public $ResponseCode;
public $Redirect_uri;
private $_AuthorizeUrl = "http://api.crowdpanthers.com/oauth2/authorize";
private $_AccessTokenUrl = "http://api.crowdpanthers.com/oauth2/token";
private $_ProfileOfMe = "http://api.crowdpanthers.com/api/v1";
public function __construct() {
$this->CurlHeaders = array();
$this->ResponseCode = 0;
}
public function RequestAccessCode ($client_id, $redirect_url) {
return($this->_AuthorizeUrl . "?client_id=" . $client_id . "&response_type=code&redirect_uri=" . $redirect_url);
}
// Convert an authorization code from an Crowdpanther callback into an access token.
public function GetAccessToken($client_id, $client_secret, $auth_code) {
// Init cUrl.
$r = $this->InitCurl($this->_AccessTokenUrl);
// Add client ID and client secret to the headers.
curl_setopt($r, CURLOPT_HTTPHEADER, array (
"Authorization: Basic " . base64_encode($client_id . ":" . $client_secret),
));
// Assemble POST parameters for the request.
$post_fields = "grant_type=authorization_code&code=".$auth_code."&redirect_uri=".$this->Redirect_uri."&client_id=".$client_id."&client_secret=".$client_secret;
// Obtain and return the access token from the response.
curl_setopt($r, CURLOPT_POST, true);
curl_setopt($r, CURLOPT_POSTFIELDS, $post_fields);
$response = curl_exec($r);
if ($response == false) {
die("1- curl_exec() failed. Error: " . curl_error($r)."<br />".$response);
}
//Parse JSON return object.
return json_decode($response);
}
private function InitCurl($url) {
$r = null;
$r = curl_init();
curl_setopt($r, CURLOPT_URL, $url);
// curl_setopt($r, CURLOPT_HEADER, 1);
curl_setopt($r, CURLOPT_RETURNTRANSFER, 1);
return($r);
}
// A generic function that executes an Crowdpanthers API request.
public function ExecRequest($resource = '/me', $access_token, $get_params = array()) {
switch ($resource) {
case '/me':
$url = $this->_ProfileOfMe.$resource;
break;
default:
$url = $this->_ProfileOfMe.$resource;
break;
}
// Create request string.
$get_params['access_token'] = $access_token;
$full_url = $url."?".http_build_query($get_params);
$r = $this->InitCurl($full_url);
$headers = array(
"Accept: application/json",
);
curl_setopt($r, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($r);
$res_obj = json_decode($response);
if ($response == false) {
die("2- curl_exec() failed. Error: " . curl_error($r)."<br />".print_r($response,true));
}
//Parse JSON return object.
return json_decode($response);
}
}
?>