3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Basic client class for Yammer's OAuth/JSON API.
23 * @package YammerImportPlugin
24 * @author Brion Vibber <brion@status.net>
28 protected $apiBase = "https://www.yammer.com";
29 protected $consumerKey, $consumerSecret;
30 protected $token, $tokenSecret, $verifier;
32 public function __construct($consumerKey, $consumerSecret, $token=null, $tokenSecret=null)
34 $this->consumerKey = $consumerKey;
35 $this->consumerSecret = $consumerSecret;
36 $this->token = $token;
37 $this->tokenSecret = $tokenSecret;
41 * Make an HTTP GET request with OAuth headers and return an HTTPResponse
42 * with the returned body and codes.
45 * @return HTTPResponse
47 * @throws Exception on low-level network error
49 protected function httpGet($url)
51 $headers = array('Authorization: ' . $this->authHeader());
53 $client = HTTPClient::start();
54 return $client->get($url, $headers);
58 * Make an HTTP GET request with OAuth headers and return the response body
64 * @throws Exception on low-level network or HTTP error
66 public function fetchUrl($url)
68 $response = $this->httpGet($url);
69 if ($response->isOk()) {
70 return $response->getBody();
72 throw new Exception("Yammer API returned HTTP code " . $response->getStatus() . ': ' . $response->getBody());
77 * Make an HTTP hit with OAuth headers and return the response body on success.
79 * @param string $path URL chunk for the API method
80 * @param array $params
83 * @throws Exception on low-level network or HTTP error
85 protected function fetchApi($path, $params=array())
87 $url = $this->apiBase . '/' . $path;
89 $url .= '?' . http_build_query($params, null, '&');
91 return $this->fetchUrl($url);
95 * Hit the main Yammer API point and decode returned JSON data.
97 * @param string $method
98 * @param array $params
99 * @return array from JSON data
101 * @throws Exception for HTTP error or bad JSON return
103 protected function api($method, $params=array())
105 $body = $this->fetchApi("api/v1/$method.json", $params);
106 $data = json_decode($body, true);
108 throw new Exception("Invalid JSON response from Yammer API");
114 * Build an Authorization header value from the keys we have available.
116 protected function authHeader()
120 $params = array('realm' => '',
121 'oauth_consumer_key' => $this->consumerKey,
122 'oauth_signature_method' => 'PLAINTEXT',
123 'oauth_timestamp' => time(),
124 'oauth_nonce' => time(),
125 'oauth_version' => '1.0');
127 $params['oauth_token'] = $this->token;
129 if ($this->tokenSecret) {
130 $params['oauth_signature'] = $this->consumerSecret . '&' . $this->tokenSecret;
132 $params['oauth_signature'] = $this->consumerSecret . '&';
134 if ($this->verifier) {
135 $params['oauth_verifier'] = $this->verifier;
137 $parts = array_map(array($this, 'authHeaderChunk'), array_keys($params), array_values($params));
138 return 'OAuth ' . implode(', ', $parts);
145 protected function authHeaderChunk($key, $val)
147 return urlencode($key) . '="' . urlencode($val) . '"';
151 * @return array of oauth return data; should contain nice things
153 public function requestToken()
155 if ($this->token || $this->tokenSecret) {
156 throw new Exception("Requesting a token, but already set up with a token");
158 $data = $this->fetch('oauth/request_token');
160 parse_str($data, $arr);
165 * @return array of oauth return data; should contain nice things
167 public function accessToken($verifier)
169 $this->verifier = $verifier;
170 $data = $this->fetch('oauth/access_token');
171 $this->verifier = null;
173 parse_str($data, $arr);
178 * Give the URL to send users to to authorize a new app setup
180 * @param string $token as returned from accessToken()
183 public function authorizeUrl($token)
185 return $this->apiBase . '/oauth/authorize?oauth_token=' . urlencode($token);
189 * High-level API hit: fetch all messages in the network (up to 20 at a time).
190 * Return data is the full JSON array returned, including meta and references
193 * The matching messages themselves will be in the 'messages' item within.
195 * @param array $options optional set of additional params for the request.
198 * @throws Exception on low-level or HTTP error
200 public function messages($params=array())
202 return $this->api('messages', $params);
206 * High-level API hit: fetch all users in the network (up to 50 at a time).
207 * Return data is the full JSON array returned, listing user items.
209 * The matching messages themselves will be in the 'users' item within.
211 * @param array $options optional set of additional params for the request.
212 * @return array of JSON-sourced user data arrays
214 * @throws Exception on low-level or HTTP error
216 public function users($params=array())
218 return $this->api('users', $params);