3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for doing OAuth calls as a consumer
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once 'OAuth.php';
37 * Exception wrapper for cURL errors
39 * @category Integration
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
46 class OAuthClientException extends Exception
51 * Base class for doing OAuth calls as a consumer
53 * @category Integration
55 * @author Zach Copley <zach@status.net>
56 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57 * @link http://status.net/
68 * Can be initialized with just consumer key and secret for requesting new
69 * tokens or with additional request token or access token
71 * @param string $consumer_key consumer key
72 * @param string $consumer_secret consumer secret
73 * @param string $oauth_token user's token
74 * @param string $oauth_token_secret user's secret
78 function __construct($consumer_key, $consumer_secret,
79 $oauth_token = null, $oauth_token_secret = null)
81 $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82 $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
85 if (isset($oauth_token) && isset($oauth_token_secret)) {
86 $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
91 * Gets a request token from the given url
93 * @param string $url OAuth endpoint for grabbing request tokens
94 * @param string $callback authorized request token callback
96 * @return OAuthToken $token the request token
98 function getRequestToken($url, $callback = null)
102 if (!is_null($callback)) {
103 $params['oauth_callback'] = $callback;
106 $response = $this->oAuthGet($url, $params);
109 parse_str($response, $arr);
111 $token = $arr['oauth_token'];
112 $secret = $arr['oauth_token_secret'];
113 $confirm = $arr['oauth_callback_confirmed'];
115 if (isset($token) && isset($secret)) {
117 $token = new OAuthToken($token, $secret);
119 if (isset($confirm)) {
120 if ($confirm == 'true') {
121 common_debug('Twitter bridge - callback confirmed.');
124 throw new OAuthClientException(
125 'Callback was not confirmed by Twitter.'
131 throw new OAuthClientException(
132 'Could not get a request token from Twitter.'
138 * Builds a link that can be redirected to in order to
139 * authorize a request token.
141 * @param string $url endpoint for authorizing request tokens
142 * @param OAuthToken $request_token the request token to be authorized
144 * @return string $authorize_url the url to redirect to
146 function getAuthorizeLink($url, $request_token)
148 $authorize_url = $url . '?oauth_token=' .
151 return $authorize_url;
155 * Fetches an access token
157 * @param string $url OAuth endpoint for exchanging authorized request tokens
159 * @param string $verifier 1.0a verifier
161 * @return OAuthToken $token the access token
163 function getAccessToken($url, $verifier = null)
167 if (!is_null($verifier)) {
168 $params['oauth_verifier'] = $verifier;
171 $response = $this->oAuthPost($url, $params);
174 parse_str($response, $arr);
176 $token = $arr['oauth_token'];
177 $secret = $arr['oauth_token_secret'];
179 if (isset($token) && isset($secret)) {
180 $token = new OAuthToken($token, $secret);
183 throw new OAuthClientException(
184 'Could not get a access token from Twitter.'
190 * Use HTTP GET to make a signed OAuth requesta
192 * @param string $url OAuth request token endpoint
193 * @param array $params additional parameters
195 * @return mixed the request
197 function oAuthGet($url, $params = null)
199 $request = OAuthRequest::from_consumer_and_token($this->consumer,
200 $this->token, 'GET', $url, $params);
201 $request->sign_request($this->sha1_method,
202 $this->consumer, $this->token);
204 return $this->httpRequest($request->to_url());
208 * Use HTTP POST to make a signed OAuth request
210 * @param string $url OAuth endpoint
211 * @param array $params additional post parameters
213 * @return mixed the request
215 function oAuthPost($url, $params = null)
217 $request = OAuthRequest::from_consumer_and_token($this->consumer,
218 $this->token, 'POST', $url, $params);
219 $request->sign_request($this->sha1_method,
220 $this->consumer, $this->token);
222 return $this->httpRequest($request->get_normalized_http_url(),
223 $request->to_postdata());
227 * Make a HTTP request.
229 * @param string $url Where to make the
230 * @param array $params post parameters
232 * @return mixed the request
234 function httpRequest($url, $params = null)
236 $request = new HTTPClient($url);
237 $request->setConfig(array(
238 'connect_timeout' => 120,
240 'follow_redirects' => true,
241 'ssl_verify_peer' => false,
242 'ssl_verify_host' => false
245 // Twitter is strict about accepting invalid "Expect" headers
246 $request->setHeader('Expect', '');
248 if (isset($params)) {
249 $request->setMethod(HTTP_Request2::METHOD_POST);
250 $request->setBody($params);
254 $response = $request->send();
255 $code = $response->getStatus();
256 if ($code < 200 || $code >= 400) {
257 throw new OAuthClientException($response->getBody(), $code);
259 return $response->getBody();
260 } catch (Exception $e) {
261 throw new OAuthClientException($e->getMessage(), $e->getCode());