function maybe_refresh_token() { global $wpdb; if (empty($this->token->token) || empty($this->token->token->tokenExpires)) { return; } if ($this->token->token->tokenExpires && $this->token->token->tokenExpires < time()) { $api_url = 'https://api.login.yahoo.com/oauth/v2/get_token'; $api_url .= '?oauth_session_handle=' . $this->token->token->sessionHandle; $refresh = $this->request($api_url, array('method' => 'GET', 'raw_response' => true)); if (!Keyring_Util::is_error($refresh)) { $token = $this->parse_access_token($refresh); // Fake request token global $keyring_request_token; $keyring_request_token = new Keyring_Request_Token($this->get_name(), array()); // Build (real) access token $access_token = new Keyring_Access_Token($this->get_name(), new OAuthToken($token['oauth_token'], $token['oauth_token_secret']), $this->build_token_meta($token), $this->token->unique_id); // Store the updated access token $access_token = apply_filters('keyring_access_token', $access_token, $token); $id = $this->store->update($access_token); // And switch to using it $this->set_token($access_token); } } }
function test_connection() { $res = $this->request('http://api.tumblr.com/v2/user/info', array('method' => 'POST')); if (!Keyring_Util::is_error($res)) { return true; } return $res; }
function test_connection() { $res = $this->request('https://api.twitter.com/1.1/account/verify_credentials.json'); if (!Keyring_Util::is_error($res)) { return true; } return $res; }
function test_connection() { $response = $this->request($this->self_url, array('method' => $this->self_method)); if (!Keyring_Util::is_error($response)) { return true; } return $response; }
function test_connection() { $res = $this->request("https://api.linkedin.com/v1/people/~:(id,formatted-name)?format=json"); if (!Keyring_Util::is_error($res)) { return true; } return $res; }
function test_connection() { $response = $this->request('https://api.del.icio.us/v1/posts/all?results=1'); if (!Keyring_Util::is_error($response)) { return true; } return $response; }
function build_token_meta($token) { $meta = array('user_id' => $token['user_id'], 'refresh_token' => $token['refresh_token'], 'expires' => time() + $token['expires_in'], '_classname' => get_called_class()); $this->set_token(new Keyring_Access_Token($this->get_name(), $token['access_token'], array())); $response = $this->request($this->profile_url); if (!Keyring_Util::is_error($response)) { $meta['first_date'] = $response->profile->firstDate; } return apply_filters('keyring_access_token_meta', $meta, self::NAME, $token, array(), $this); }
function build_token_meta($token) { $token = new Keyring_Access_Token($this->get_name(), $token['access_token'], array()); $this->set_token($token); $res = $this->request($this->self_url, array('method' => $this->self_method)); if (Keyring_Util::is_error($res)) { $meta = array(); } else { $meta = array('user_id' => $res->response->user->id, 'first_name' => $res->response->user->firstName, 'last_name' => $res->response->user->lastName, 'picture' => $res->response->user->photo->prefix . '300x300' . $res->response->user->photo->suffix); } return apply_filters('keyring_access_token_meta', $meta, 'foursquare', $token, $res, $this); }
function build_token_meta($token) { // Need to make a request to get full information $this->set_token(new Keyring_Access_Token($this->get_name(), new OAuthToken($token['oauth_token'], $token['oauth_token_secret']))); $url = "https://api.flickr.com/services/rest/?"; $params = array('method' => 'flickr.people.getInfo', 'api_key' => $this->key, 'user_id' => $token['user_nsid']); $url = $url . http_build_query($params); $response = $this->request($url, array('method' => 'GET')); if (Keyring_Util::is_error($response)) { $meta = array(); } else { $meta = array('user_id' => $token['user_nsid'], 'username' => $token['username'], 'name' => $token['fullname'], 'picture' => "http://farm{$response->person->iconfarm}.staticflickr.com/{$response->person->iconserver}/buddyicons/{$token['user_nsid']}.jpg"); } return apply_filters('keyring_access_token_meta', $meta, 'flickr', $token, $response, $this); }
function test_connection() { $res = $this->request('https://api.twitter.com/1.1/account/verify_credentials.json'); if (!Keyring_Util::is_error($res)) { return true; } // Twitter may return a rate limiting error if the user accesses the sharing settings or post // page frequently. If so, ignore that error, things are likely aaaa-okay... $keyring_error_message = $res->get_error_message(); if (is_array($keyring_error_message) && isset($keyring_error_message['response']['code'])) { if (429 == absint($keyring_error_message['response']['code'])) { return true; } } return $res; }
function build_token_meta($token) { $this->set_token(new Keyring_Access_Token($this->get_name(), $token['access_token'], array())); $response = $this->request($this->user_url, array('method' => $this->user_method)); if (Keyring_Util::is_error($response)) { $meta = array(); } else { // Only useful thing in that request is userID $meta = array('user_id' => (int) $response->userID); // Now get the rest of their profile $profile = $this->request($this->profile_url, array('method' => $this->profile_method)); if (!Keyring_Util::is_error($profile)) { $meta['username'] = substr($profile->profile, strrpos($profile->profile, '/') + 1); $meta['name'] = $profile->name; $meta['picture'] = $profile->large_picture; } return apply_filters('keyring_access_token_meta', $meta, 'runkeeper', $token, $profile, $this); } return array(); }
function verify_token() { Keyring_Util::debug('Keyring_Service_OAuth1::verify_token()'); if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], 'keyring-verify-' . $this->get_name())) { Keyring::error(__('Invalid/missing verification nonce.', 'keyring')); exit; } // Load up the request token that got us here and globalize it if (isset($_GET['state'])) { global $keyring_request_token; $state = preg_replace('/[^\\x20-\\x7E]/', '', $_GET['state']); $keyring_request_token = $this->store->get_token(array('id' => $state, 'type' => 'request')); Keyring_Util::debug('OAuth1 Loaded Request Token ' . $state); Keyring_Util::debug($keyring_request_token); $secret = $keyring_request_token->token['oauth_token_secret']; // Remove request token, don't need it any more. $this->store->delete(array('id' => $state, 'type' => 'request')); } // Get an access token, using the temporary token passed back $token = isset($_GET['oauth_token']) ? $_GET['oauth_token'] : false; $access_token_url = $this->access_token_url; if (!empty($_GET['oauth_verifier'])) { $access_token_url = add_query_arg(array('oauth_verifier' => urlencode($_GET['oauth_verifier'])), $access_token_url); } // Set up a consumer token and make the request for an access_token $token = new OAuthConsumer($token, $secret); $this->set_token(new Keyring_Access_Token($this->get_name(), $token, array())); $res = $this->request($access_token_url, array('method' => $this->access_token_method, 'raw_response' => true)); Keyring_Util::debug('OAuth1 Access Token Response'); Keyring_Util::debug($res); if (!Keyring_Util::is_error($res)) { $token = $this->parse_access_token($res); $access_token = new Keyring_Access_Token($this->get_name(), new OAuthToken($token['oauth_token'], $token['oauth_token_secret']), $this->build_token_meta($token)); $access_token = apply_filters('keyring_access_token', $access_token, $token); Keyring_Util::debug('OAuth1 Access Token for storage'); Keyring_Util::debug($access_token); $id = $this->store_token($access_token); $this->verified($id, $keyring_request_token); exit; } else { Keyring::error(sprintf(__('There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'keyring'), $this->get_label())); return false; } }
function test_connection() { $url = "https://api.flickr.com/services/rest/?"; $params = array('method' => 'flickr.test.login', 'api_key' => $this->key); $url = $url . http_build_query($params); $response = $this->request($url); if (!Keyring_Util::is_error($response)) { return true; } return $response; }
/** * Handle a cron request to import "the latest" content for this importer. Should * rely solely on database state of some sort, since nothing is passed in. Make * sure to also update anything in the DB required for the next run. If you set up your * other methods "discretely" enough, you might not need to override this. */ function do_auto_import() { defined('WP_IMPORTING') or define('WP_IMPORTING', true); do_action('import_start'); set_time_limit(0); // In case auto-import has been disabled, clear all jobs and bail if (!$this->get_option('auto_import')) { wp_clear_scheduled_hook('keyring_' . static::SLUG . '_import_auto'); return; } // Need a token to do anything with this if (!$this->service->get_token()) { return; } require_once ABSPATH . 'wp-admin/includes/import.php'; require_once ABSPATH . 'wp-admin/includes/post.php'; $this->auto_import = true; $num = 0; while (!$this->finished && $num < static::REQUESTS_PER_LOAD) { $data = $this->make_request(); if (Keyring_Util::is_error($data)) { return; } $result = $this->extract_posts_from_data($data); if (Keyring_Util::is_error($result)) { return; } $result = $this->insert_posts(); if (Keyring_Util::is_error($result)) { return; } // Keep track of which "page" we're up to, in case an auto importer cares $this->set_option('page', $this->get_option('page') + 1); $num++; } do_action('import_end'); }
function verify_token() { if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], 'keyring-verify-' . $this->get_name())) { Keyring::error(__('Invalid/missing verification nonce.', 'keyring')); exit; } // Load up the request token that got us here and globalize it if (isset($_REQUEST['state'])) { global $keyring_request_token; $state = preg_replace('/[^\\x20-\\x7E]/', '', $_GET['state']); $keyring_request_token = $this->store->get_token(array('id' => $state, 'type' => 'request')); Keyring_Util::debug('HTTP Basic Loaded Request Token ' . $state); Keyring_Util::debug($keyring_request_token); // Remove request token, don't need it any more. $this->store->delete(array('id' => $state, 'type' => 'request')); } if (!strlen($_POST['username'])) { $url = Keyring_Util::admin_url($this->get_name(), array('action' => 'request', 'error' => 'empty', 'kr_nonce' => wp_create_nonce('keyring-request'))); Keyring_Util::debug($url); wp_safe_redirect($url); exit; } // HTTP Basic does not use Keyring_Request_Tokens, since there's only one step $token = new Keyring_Access_Token($this->get_name(), base64_encode($_POST['username'] . ':' . $_POST['password'])); $this->set_token($token); $res = $this->request($this->verify_url, array('method' => $this->verify_method)); // We will get a 401 if they entered an incorrect user/pass combo. ::request // will then return a Keyring_Error if (Keyring_Util::is_error($res)) { $url = Keyring_Util::admin_url($this->get_name(), array('action' => 'request', 'error' => '401', 'kr_nonce' => wp_create_nonce('keyring-request'))); Keyring_Util::debug($url); wp_safe_redirect($url); exit; } $meta = array_merge(array('username' => $_POST['username']), $this->build_token_meta($token)); $access_token = new Keyring_Access_Token($this->get_name(), $token, $meta); $access_token = apply_filters('keyring_access_token', $access_token, array()); // If we didn't get a 401, then we'll assume it's OK $id = $this->store_token($access_token); $this->verified($id, $keyring_request_token); }