-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhubspot-oauth.php
182 lines (161 loc) · 5.98 KB
/
hubspot-oauth.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* HubSpot OAuth Client Test Mule
*
* This is a pretty simple test that will prompt you to authenticate into
* your HubSpot portal, and then will pull the first page of contacts or
* will tell you that it didn't work. Fill in your hub/portal ID and
* client ID below, and then point the built-in PHP webserver at this. The
* recommended command is this:
* $ php -S localhost:3333
*
* (If you're not using the built-in PHP web server, update myUri to be a
* correct link for your setup.) Then, just go to
* http://localhost:3333/hubspot-oauth.php
*
* to run the test. You should immediately be punted into HubSpot, where it
* will ask you to authorize your app (or ask you to log in first).
*
* @package jkachel/hubspot-oauth
* @author James Kachel <[email protected]>
* @license MIT
* @copyright 2016
*/
require_once('vendor/autoload.php');
use HubspotOauth\Authenticate\Authenticate;
use HubspotOauth\Entities\ContactProperty;
use Carbon\Carbon;
/**
* Specify your Portal ID (Hub ID) and Client ID here.
*/
$myHubId = '2198964';
$myClientId = '01be8aa1-f145-11e5-b977-4541e4d901e9';
$myUri = 'http://localhost:3333/hubspot-oauth.php';
function displayHeader() {
?><!DOCTYPE html>
<html>
<head>
<title>Hubspot OAuth Tester</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="/hubspot-oauth.php">Hubspot OAuth Tester <span class="sr-only">(current)</span></a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<?php
}
function displayFooter() {
?>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.3/jquery.min.map"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html><?php
}
function displayError($e) {
?><div class="container-fluid">
<div class="row">
<div class="col-md-10 col-md-offset-1 text-center">
<h1 class="text-danger">It Didn't Work</h1>
<p>The error was: <?php echo $e; ?></p>
</div>
</div>
</div>
<?php
}
$auth = new Authenticate($myHubId, $myClientId);
if(empty($_GET) && empty($_POST)) {
$url = $auth->initiate($myUri, 'contacts-ro+offline');
trigger_error('Redirecting to: '.$url, E_USER_NOTICE);
header('Location: '.$url);
return;
}
displayHeader();
if(empty($_POST) && $auth->process($_GET)) {
try {
$resp = $auth->call('get', '/contacts/v1/lists/all/contacts/all');
$data = $resp['response']['contacts'];
$resp2 = new ContactProperty($auth);
$resp2->name = md5(time());
$resp2->label = 'This is a test property';
$resp2->description = 'Whee, testing!';
$resp2->groupName = 'Testing';
$resp2->type = 'string';
$resp2->fieldType = 'text';
$resp2->formField = true;
$resp2->displayOrder = 6;
$resp2->options = [];
$resp2->save();
?>
<div class="container-fluid">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="text-success">It Worked!</h1>
<table class="table table-compact table-bordered table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Nmae</th>
<th>VID</th>
<th>Added At</th>
</tr>
</thead>
<tbody>
<?php for($i = 0; $i < count($data); $i++): ?>
<tr>
<td>
<?php echo $data[$i]['properties']['firstname']['value']; ?>
</td>
<td>
<?php echo $data[$i]['properties']['lastname']['value']; ?>
</td>
<td>
<?php echo $data[$i]['vid']; ?>
</td>
<td>
<?php echo Carbon::createFromTimestamp($data[$i]['addedAt'])->format('Y-m-d H:i:s'); ?>
</td>
</tr>
<?php endfor; ?>
</tbody>
</table>
<p>Raw data:<br />
<pre>
<?php var_dump($data); ?>
<?php var_dump($resp); ?>
</pre></p>
</div>
</div>
</div>
<?php
} catch(\Exception $e) {
displayError($e);
}
} else {
?><div class="container-fluid">
<div class="row">
<div class="col-md-10 col-md-offset-1 text-center">
<h1 class="text-danger">It Didn't Work</h1>
</div>
</div>
</div>
<?php
}
displayFooter();