set_endpoint( 'authorize', 'https://accounts.google.com/o/oauth2/auth', 'GET' ); $this->set_endpoint( 'access_token', 'https://accounts.google.com/o/oauth2/token', 'POST' ); $this->set_endpoint( 'self', 'https://www.googleapis.com/oauth2/v1/userinfo', 'GET' ); $creds = $this->get_credentials(); $this->redirect_uri = $creds['redirect_uri']; $this->key = $creds['key']; $this->secret = $creds['secret']; $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->authorization_header = 'Bearer'; // Oh, you $this->authorization_parameter = false; // Need to reset the callback because Google is very strict about where it sends people if ( !empty( $creds['redirect_uri'] ) ) $this->callback_url = $creds['redirect_uri']; // Allow user to manually enter a redirect URI else $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url ); // At least strip nonces, since you can't save them in your app config } function basic_ui_intro() { echo '
' . sprintf( __( "Google controls access to all of their APIs through their API Console. Go to the console and click the project dropdown just under the logo in the upper left of the screen. Click Create… to create a new project. Enter a name and then click Create project. You don't technically need access to any of the additional APIs, but if you want to, then feel free to enable them", 'keyring' ), 'https://code.google.com/apis/console' ) . '
'; echo '' . __( "Now you need to set up an OAuth Client ID.", 'keyring' ) . '
'; echo '%s
", 'keyring' ), Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . 'http://%s
", 'keyring' ), $_SERVER['HTTP_HOST'] ) . '' . __( "Once you've saved your details, copy the Client ID into the Client ID field below, and the Client secret value into Client Secret. The Redirect URI box should fill itself out for you.", 'keyring' ) . '
'; } function _get_credentials() { if ( defined( 'KEYRING__GOOGLECONTACTS_KEY' ) && defined( 'KEYRING__GOOGLECONTACTS_SECRET' ) ) { return array( 'redirect_uri' => defined( 'KEYRING__GOOGLECONTACTS_URI' ) ? constant( 'KEYRING__GOOGLECONTACTS_URI' ) : '', // optional 'key' => constant( 'KEYRING__GOOGLECONTACTS_KEY' ), 'secret' => constant( 'KEYRING__GOOGLECONTACTS_SECRET' ), ); } else { return null; } } function request_token_params( $params ) { $params['scope'] = self::SCOPE; return $params; } function redirect_incoming_verify( $request ) { if ( !isset( $request['kr_nonce'] ) ) { // First request, from Google. Nonce it and move on. $kr_nonce = wp_create_nonce( 'keyring-verify' ); $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify', 'kr_nonce' => $kr_nonce, 'nonce' => $nonce, 'state' => $request['state'], 'code' => $request['code'], // Auth code from successful response (maybe) ) ) ); exit; } } function build_token_meta( $token ) { $meta = array(); if ( !$token ) return $meta; $token = new Keyring_Access_Token( $this->get_name(), new OAuthToken( $token['access_token'], '' ), array() ); $this->set_token( $token ); $response = $this->request( $this->self_url, array( 'method' => $this->self_method ) ); if ( !Keyring_Util::is_error( $response ) ) { $meta = array( 'user_id' => $response->id, 'name' => $response->name, 'profile' => $response->link, 'picture' => $response->picture, ); } return apply_filters( 'keyring_access_token_meta', $meta, 'google-contacts', $token, $response, $this ); } function get_display( Keyring_Access_Token $token ) { return $token->get_meta( 'name' ); } function request( $url, array $params = array() ) { // add header (version), required for all requests $params['headers']['GData-Version'] = self::API_VERSION; return parent::request( $url, $params ); } // Minor modifications from Keyring_Service::basic_ui function basic_ui() { if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-manage-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing management nonce.', 'keyring' ) ); exit; } // Common Header echo '' . __( '← Back', 'keyring' ) . '
'; echo '' . __( 'Credentials saved.', 'keyring' ) . '