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 '

' . __( "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' ) . '

'; echo '

' . __( "Now you need to set up an OAuth Client ID.", 'keyring' ) . '

'; echo '
    '; echo '
  1. ' . __( "Click API Access in the menu on the left.", 'keyring' ) . '
  2. '; echo '
  3. ' . __( "Click the big blue button labelled Create an OAuth 2.0 client ID…", 'keyring' ) . '
  4. '; echo '
  5. ' . __( "You must enter a Product name, but you can skip the logo and home page URL", 'keyring' ) . '
  6. '; echo '
  7. ' . __( "Leave the Application type set to Web application", 'keyring' ) . '
  8. '; echo '
  9. ' . __( "Next to Your site or hostname, click (more options) %s", 'keyring' ) . '
  10. '; echo '
  11. ' . sprintf( __( "In the Authorized Redirect URIs box, enter the URL %s", 'keyring' ), Keyring_Util::admin_url( 'google', array( 'action' => 'verify' ) ) ) . '
  12. '; echo '
  13. ' . sprintf( __( "For the Authorized JavaScript Origins, enter the URL of your domain, e.g. http://%s", 'keyring' ), $_SERVER['HTTP_HOST'] ) . '
  14. '; echo '
  15. ' . __( "Click Create client ID when you're done", 'keyring' ) . '
  16. '; echo '
'; echo '

' . __( "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, '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 '
'; screen_icon( 'ms-admin' ); echo '

' . __( 'Keyring Service Management', 'keyring' ) . '

'; echo '

' . __( '← Back', 'keyring' ) . '

'; echo '

' . sprintf( __( '%s API Credentials', 'keyring' ), esc_html( $this->get_label() ) ) . '

'; // Handle actually saving credentials if ( isset( $_POST['api_key'] ) && isset( $_POST['api_secret'] ) ) { // Store credentials against this service $this->update_credentials( array( 'key' => stripslashes( $_POST['api_key'] ), 'secret' => stripslashes( $_POST['api_secret'] ), 'redirect_uri' => stripslashes( $_POST['redirect_uri'] ), ) ); echo '

' . __( 'Credentials saved.', 'keyring' ) . '

'; } $api_key = $api_secret = $redirect_uri = ''; if ( $creds = $this->get_credentials() ) { $api_key = $creds['key']; $api_secret = $creds['secret']; $redirect_uri = $creds['redirect_uri']; } echo apply_filters( 'keyring_' . $this->get_name() . '_basic_ui_intro', '' ); if ( ! $redirect_uri ) $redirect_uri = Keyring_Util::admin_url( 'google', array( 'action' => 'verify' ) ); // Output basic form for collecting key/secret echo '
'; echo ''; echo ''; wp_nonce_field( 'keyring-manage', 'kr_nonce', false ); wp_nonce_field( 'keyring-manage-' . $this->get_name(), 'nonce', false ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
' . __( 'Client ID', 'keyring' ) . '
' . __( 'Client Secret', 'keyring' ) . '
' . __( 'Redirect URI', 'keyring' ) . '
'; echo '

'; echo ''; echo '' . __( 'Cancel', 'keyring' ) . ''; echo '

'; echo '
'; echo '
'; } function test_connection() { $res = $this->request( $this->self_url, array( 'method' => $this->self_method ) ); if ( !Keyring_Util::is_error( $res ) ) return true; return $res; } } add_action( 'keyring_load_services', array( 'Keyring_Service_GoogleContacts', 'init' ) );