/** * Perform an authorization-related request. * * @param array an associative array of auth settings * @param string the HTTP request method * @param string the URL * @param array an associative array of request parameters * @return MMI_Curl_Response */ protected function _auth_request($auth_config, $method, $url, $parms = array()) { // Create the consumer, token, and signature method objects $consumer = $this->_get_consumer($auth_config); $token = $this->_get_token($auth_config); $signature_method = $this->_get_signature_method(); // Prepare and sign the OAuth request $request = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $url, $parms); $request->sign_request($signature_method, $consumer, $token); if (strtoupper($method) === MMI_HTTP::METHOD_GET) { $url = $request->to_url(); } else { $url = $request->get_normalized_http_url(); $parms = $request->to_postdata(); } unset($consumer, $token, $signature_method, $request); // Execute the cURL request $method = strtolower($method); return MMI_Curl::factory()->{$method}($url, $parms); }
/** * Exchange the request token for an access token. * * @throws Kohana_Exception * @param string the verification code * @param array an associative array of auth settings * @return object */ public function get_access_token($auth_verifier = NULL, $auth_config = array()) { // Configure the auth settings if (!is_array($auth_config)) { $auth_config = array(); } $auth_config = Arr::merge($this->_auth_config, $auth_config); // Configure the HTTP method and the URL $http_method = MMI_HTTP::METHOD_POST; $url = $this->_api_url; $this->_ensure_parm('Access token URL', $url); // Get the API key $api_key = $this->_api_key; $this->_ensure_parm('API key', $api_key); // Configure the request parameters $frob = Arr::get($auth_config, 'token_key'); $parms = array('api_key' => $api_key, 'format' => 'json', 'frob' => $frob, 'nojsoncallback' => 1, 'method' => 'flickr.auth.getToken'); $parms['api_sig'] = $this->_get_signature($parms); // Execute the cURL request $http_method = strtolower($http_method); $response = MMI_Curl::factory()->{$http_method}($url, $parms); // Extract the token $token = NULL; if ($this->_validate_curl_response($response, 'Invalid access token')) { $token = $this->_extract_access_token($response); } return $token; }
/** * Log the user into Reddit using the username and password from the config * file. Save the cookie and user modhash in the corresponding class * properties. * * @param boolean save the cookie to the database? * @return void */ public function login($save_cookie_to_db = TRUE) { // Get username and password $auth_config = $this->_auth_config; $username = Arr::get($auth_config, 'username'); $this->_ensure_parm('Username', $username); $password = Arr::get($auth_config, 'password'); $this->_ensure_parm('Password', $password); // Login $url = $this->_api_url . 'api/login/memakeit'; $response = MMI_Curl::factory()->post($url, array('api_type' => 'json', 'user' => $username, 'passwd' => $password)); $response = $response instanceof MMI_Curl_Response ? $response->body() : NULL; $data = NULL; if (!empty($response)) { $response = $this->_decode_json($response, TRUE); $data = Arr::path($response, 'json.data'); } // Extract cookie and user modhash $cookie = NULL; $usermodhash = NULL; if (!empty($data)) { $cookie = Arr::get($data, 'cookie'); $usermodhash = Arr::get($data, 'modhash'); } // Save cookie and user modhash if (!empty($cookie) and !empty($usermodhash)) { if ($save_cookie_to_db) { $this->_save_cookie_to_db($cookie, $usermodhash); } $this->_cookie = $cookie; $this->_usermodhash = $usermodhash; } }