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 // TRANS: Exeption thrown when an external Yammer system gives an error.
73 // TRANS: %1$s is an HTTP error code, %2$s is the error message body.
74 throw new Exception(sprintf(_m('Yammer API returned HTTP code %1$s: %2$s'),
75 $response->getStatus(),
76 $response->getBody()));
81 * Make an HTTP hit with OAuth headers and return the response body on success.
83 * @param string $path URL chunk for the API method
84 * @param array $params
87 * @throws Exception on low-level network or HTTP error
89 protected function fetchApi($path, $params=array())
91 $url = $this->apiBase . '/' . $path;
93 $url .= '?' . http_build_query($params, null, '&');
95 return $this->fetchUrl($url);
99 * Hit the main Yammer API point and decode returned JSON data.
101 * @param string $method
102 * @param array $params
103 * @return array from JSON data
105 * @throws Exception for HTTP error or bad JSON return
107 public function api($method, $params=array())
109 $body = $this->fetchApi("api/v1/$method.json", $params);
110 $data = json_decode($body, true);
111 if ($data === null) {
112 common_log(LOG_ERR, "Invalid JSON response from Yammer API: " . $body);
113 // TRANS: Exeption thrown when an external Yammer system an invalid JSON response.
114 throw new Exception(_m('Invalid JSON response from Yammer API.'));
120 * Build an Authorization header value from the keys we have available.
122 protected function authHeader()
126 $params = array('realm' => '',
127 'oauth_consumer_key' => $this->consumerKey,
128 'oauth_signature_method' => 'PLAINTEXT',
129 'oauth_timestamp' => time(),
130 'oauth_nonce' => time(),
131 'oauth_version' => '1.0');
133 $params['oauth_token'] = $this->token;
135 if ($this->tokenSecret) {
136 $params['oauth_signature'] = $this->consumerSecret . '&' . $this->tokenSecret;
138 $params['oauth_signature'] = $this->consumerSecret . '&';
140 if ($this->verifier) {
141 $params['oauth_verifier'] = $this->verifier;
143 $parts = array_map(array($this, 'authHeaderChunk'), array_keys($params), array_values($params));
144 return 'OAuth ' . implode(', ', $parts);
148 * Encode a key-value pair for use in an authentication header.
154 protected function authHeaderChunk($key, $val)
156 return urlencode($key) . '="' . urlencode($val) . '"';
160 * Ask the Yammer server for a request token, which can be passed on
161 * to authorizeUrl() for the user to start the authentication process.
163 * @return array of oauth return data; should contain nice things
165 public function requestToken()
167 if ($this->token || $this->tokenSecret) {
168 // TRANS: Exeption thrown when a trust relationship has already been established.
169 throw new Exception(_m('Requesting a token, but already set up with a token.'));
171 $data = $this->fetchApi('oauth/request_token');
173 parse_str($data, $arr);
178 * Get a final access token from the verifier/PIN code provided to
179 * the user from Yammer's auth pages.
181 * @return array of oauth return data; should contain nice things
183 public function accessToken($verifier)
185 $this->verifier = $verifier;
186 $data = $this->fetchApi('oauth/access_token');
187 $this->verifier = null;
189 parse_str($data, $arr);
194 * Give the URL to send users to to authorize a new app setup.
196 * @param string $token as returned from accessToken()
199 public function authorizeUrl($token)
201 return $this->apiBase . '/oauth/authorize?oauth_token=' . urlencode($token);
205 * High-level API hit: fetch all messages in the network (up to 20 at a time).
206 * Return data is the full JSON array returned, including meta and references
209 * The matching messages themselves will be in the 'messages' item within.
211 * @param array $options optional set of additional params for the request.
214 * @throws Exception on low-level or HTTP error
216 public function messages($params=array())
218 return $this->api('messages', $params);
222 * High-level API hit: fetch all users in the network (up to 50 at a time).
223 * Return data is the full JSON array returned, listing user items.
225 * The matching messages themselves will be in the 'users' item within.
227 * @param array $options optional set of additional params for the request.
228 * @return array of JSON-sourced user data arrays
230 * @throws Exception on low-level or HTTP error
232 public function users($params=array())
234 return $this->api('users', $params);
238 * High-level API hit: fetch all groups in the network (up to 20 at a time).
239 * Return data is the full JSON array returned, listing user items.
241 * The matching messages themselves will be in the 'users' item within.
243 * @param array $options optional set of additional params for the request.
244 * @return array of JSON-sourced user data arrays
246 * @throws Exception on low-level or HTTP error
248 public function groups($params=array())
250 return $this->api('groups', $params);