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() { $response = $this->request('https://api.del.icio.us/v1/posts/all?results=1'); 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() { $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() { $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() { $response = $this->request($this->self_url, array('method' => $this->self_method)); 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 token_select_box($name, $create = false) { $tokens = $this->get_tokens(); return Keyring_Util::token_select_box($tokens, $name, $create); }
static function connect_to($service, $for) { Keyring_Util::debug('Connect to: ' . $service); // Redirect into Keyring's auth handler if a valid service is provided $kr_nonce = wp_create_nonce('keyring-request'); $request_nonce = wp_create_nonce('keyring-request-' . $service); wp_safe_redirect(Keyring_Util::admin_url($service, array('action' => 'request', 'kr_nonce' => $kr_nonce, 'nonce' => $request_nonce, 'for' => $for))); exit; }
function column_actions($row) { $kr_delete_nonce = wp_create_nonce('keyring-delete'); $delete_nonce = wp_create_nonce('keyring-delete-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id()); $kr_test_nonce = wp_create_nonce('keyring-test'); $test_nonce = wp_create_nonce('keyring-test-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id()); echo '<span class="row-actions">'; echo '<span class="trash"><a href="' . Keyring_Util::admin_url(false, array('action' => 'delete', 'service' => $row->get_service()->get_name(), 'token' => $row->get_uniq_id(), 'kr_nonce' => $kr_delete_nonce, 'nonce' => $delete_nonce)) . '" title="' . esc_attr(__('Delete', 'keyring')) . '" class="delete">Delete</a></span>'; echo ' | '; echo '<a href="' . Keyring_Util::admin_url(false, array('action' => 'test', 'service' => $row->get_service()->get_name(), 'token' => $row->get_uniq_id(), 'kr_nonce' => $kr_test_nonce, 'nonce' => $test_nonce)) . '" title="' . esc_attr(__('Test', 'keyring')) . '" class="test">Test</a>'; echo '</span>'; }
function request($url, array $params = array()) { if ($this->requires_token() && empty($this->token)) { return new Keyring_Error('keyring-request-error', __('No token', 'keyring')); } $raw_response = false; if (isset($params['raw_response'])) { $raw_response = (bool) $params['raw_response']; unset($params['raw_response']); } $method = 'GET'; if (isset($params['method'])) { $method = strtoupper($params['method']); unset($params['method']); } $sign_parameters = true; if (isset($params['sign_parameters'])) { $sign_parameters = (bool) $params['sign_parameters']; unset($params['sign_parameters']); } // Should be an OAuthToken object $token = $this->token->token ? $this->token->token : null; Keyring_Util::debug($token); $sign_vars = false; if (isset($params['body']) && $sign_parameters) { if (is_string($params['body'])) { wp_parse_str($params['body'], $sign_vars); } else { if (is_array($params['body'])) { $sign_vars = $params['body']; } } } $req = $this->prepare_request($token, $method, $url, $sign_vars); $request_url = (string) $req; if ($this->token && $this->authorization_header) { $header = $req->to_header($this->authorization_realm); // Gives a complete header string, not just the second half $bits = explode(': ', $header, 2); $params['headers']['Authorization'] = $bits[1]; // This hack was introduced for Instapaper (http://stackoverflow.com/a/9645033/1507683), which is overly strict on // header formatting, but it doesn't seem to cause problems anywhere else. $params['headers']['Authorization'] = str_replace('",', '", ', $params['headers']['Authorization']); Keyring_Util::debug('OAuth1 Authorization Header'); Keyring_Util::debug($params['headers']['Authorization']); // oauth_verifier was probably added directly to the URL, need to manually remove it $request_url = remove_query_arg('oauth_verifier', $url); } $query = ''; $parsed = parse_url($request_url); if (!empty($parsed['query']) && 'POST' == $method) { $request_url = str_replace('?' . $parsed['query'], '', $request_url); $query = $parsed['query']; } Keyring_Util::debug("OAuth1 Request URL: {$request_url}"); switch ($method) { case 'GET': Keyring_Util::debug('OAuth1 GET ' . $request_url); $res = wp_remote_get($request_url, $params); break; case 'POST': $params = array_merge(array('body' => $query, 'sslverify' => false), $params); Keyring_Util::debug('OAuth1 POST ' . $request_url); Keyring_Util::debug($params); $res = wp_remote_post($request_url, $params); break; case 'PUT': $params = array_merge(array('method' => 'PUT'), $params); $res = wp_remote_request($request_url, $params); break; default: Keyring::error(__('Unsupported method specified.', 'keyring')); exit; } Keyring_Util::debug($res); $this->set_request_response_code(wp_remote_retrieve_response_code($res)); if (200 == wp_remote_retrieve_response_code($res) || 201 == wp_remote_retrieve_response_code($res)) { if ($raw_response) { return wp_remote_retrieve_body($res); } else { return $this->parse_response(wp_remote_retrieve_body($res)); } } else { return new Keyring_Error('keyring-request-error', $res); } }
/** * 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'); }
/** * Provides some direction to the user about how to configure the Spotify app */ function basic_ui_intro() { echo '<p>' . sprintf(__('To get started, <a href="%1$s">register an OAuth client on Spotify</a>. The most important setting is the <strong>Redirect URIs</strong>, which should be set to <code>%2$s</code>. You can set the other values to whatever you like.', 'keyring'), 'https://developer.spotify.com/my-applications/', Keyring_Util::admin_url('spotify', array('action' => 'verify'))) . '</p>'; echo '<p>' . __("Once you've saved those changes, copy the <strong>CLIENT ID</strong> value into the <strong>API Key</strong> field, and the <strong>CLIENT SECRET</strong> value into the <strong>API Secret</strong> field and click save.", 'keyring') . '</p>'; }
function request($url, array $params = array()) { if ($this->requires_token() && empty($this->token)) { return new Keyring_Error('keyring-request-error', __('No token')); } if ($this->requires_token()) { $params['headers'] = array('Authorization' => 'Basic ' . $this->token); } $method = 'GET'; if (isset($params['method'])) { $method = strtoupper($params['method']); unset($params['method']); } $raw_response = false; if (isset($params['raw_response'])) { $raw_response = (bool) $params['raw_response']; unset($params['raw_response']); } Keyring_Util::debug("HTTP Basic {$method} {$url}"); Keyring_Util::debug($params); switch (strtoupper($method)) { case 'GET': $res = wp_remote_get($url, $params); break; case 'POST': $res = wp_remote_post($url, $params); break; default: Keyring::error(__('Unsupported method specified for verify_token.', 'keyring')); exit; } Keyring_Util::debug($res); $this->set_request_response_code(wp_remote_retrieve_response_code($res)); if (200 == wp_remote_retrieve_response_code($res) || 201 == wp_remote_retrieve_response_code($res)) { if ($raw_response) { return wp_remote_retrieve_body($res); } else { return $this->parse_response(wp_remote_retrieve_body($res)); } } else { return new Keyring_Error('keyring-request-error', $res); } }
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; }
function basic_ui_intro() { echo '<p>' . sprintf(__('To get started, <a href="%1$s">register an OAuth client on Instagram</a>. The most important setting is the <strong>OAuth redirect_uri</strong>, which should be set to <code>%2$s</code>. You can set the other values to whatever you like.', 'keyring'), 'http://instagram.com/developer/clients/register/', Keyring_Util::admin_url('instagram', array('action' => 'verify'))) . '</p>'; echo '<p>' . __("Once you've saved those changes, copy the <strong>CLIENT ID</strong> value into the <strong>API Key</strong> field, and the <strong>CLIENT SECRET</strong> value into the <strong>API Secret</strong> field and click save (you don't need an App ID value for Instagram).", 'keyring') . '</p>'; }
function request($url, array $params = array()) { Keyring_Util::debug($url); if ($this->requires_token() && empty($this->token)) { return new Keyring_Error('keyring-request-error', __('No token')); } $token = $this->token ? $this->token : null; if (!is_null($token)) { if ($this->authorization_header) { // type can be OAuth, Bearer, ... $params['headers']['Authorization'] = $this->authorization_header . ' ' . (string) $token; } else { $url = add_query_arg(array($this->authorization_parameter => urlencode((string) $token)), $url); } } $raw_response = false; if (isset($params['raw_response'])) { $raw_response = (bool) $params['raw_response']; unset($params['raw_response']); } $method = 'GET'; if (isset($params['method'])) { $method = strtoupper($params['method']); unset($params['method']); } Keyring_Util::debug('OAuth2 Params'); Keyring_Util::debug($params); switch (strtoupper($method)) { case 'GET': $res = wp_remote_get($url, $params); break; case 'POST': $params = array_merge(array('sslverify' => false), $params); $res = wp_remote_post($url, $params); break; default: $params = array_merge(array('method' => $method, 'sslverify' => false), $params); $res = wp_remote_request($url, $params); break; } Keyring_Util::debug('OAuth2 Response'); Keyring_Util::debug($res); $this->set_request_response_code(wp_remote_retrieve_response_code($res)); if (in_array(wp_remote_retrieve_response_code($res), array(200, 201, 202))) { if ($raw_response) { return wp_remote_retrieve_body($res); } else { return $this->parse_response(wp_remote_retrieve_body($res)); } } else { return new Keyring_Error('keyring-request-error', $res); } }
/** * See __construct() for details on how this is hooked in to handle the UI for * during the verify process. */ function verify_ui() { Keyring::admin_page_header(); echo '<p>As an example, we just randomly generated a token and saved it in the token store. When you go back to your Connections listing, you should see it listed there under "Example Service".</p>'; echo '<p><a href="' . esc_url(Keyring_Util::admin_url()) . '" class="button-primary">' . __('Done', 'keyring') . '</a>'; Keyring::admin_page_footer(); }