WordPress.org

Ready to get started?Download WordPress

Forums

Keyring
[resolved] Can not store a new token in the keyring (4 posts)

  1. Federicoviola
    Member
    Posted 1 year ago #

    Hi there, I'm playing around with the keyring plugin trying to pull out data from a server that requires authorization oAuth.
    This is my code:

    class Keyring_Service_Mendeley extends Keyring_Service_OAuth1 {
    	const NAME  = 'mendeley';
    	const LABEL = 'Mendeley';
    
    	var $authorization_header;
    	var $authorization_realm;
    
    	function __construct() {
    		parent::__construct();
    
    		// Enable "basic" UI for entering key/secret
    		add_action( 'keyring_mendeley_manage_ui', array( $this, 'basic_ui' ) );
    
    		//$this->authorization_header = true;
    		$this->authorization_realm = "http://www.mendeley.com/oapi/";
    
    		$this->set_endpoint( 'request_token', 'http://www.mendeley.com/oauth/request_token/', 'GET' );
    		$this->set_endpoint( 'authorize',     'http://www.mendeley.com/oauth/authorize/', 'GET');
    		$this->set_endpoint( 'access_token',  'http://www.mendeley.com/oauth/access_token/',  'GET');
    
    		if ( defined( 'KEYRING__MENDELEY_KEY' ) && defined( 'KEYRING__MENDELEY_SECRET' ) ) {
    			$this->key = KEYRING__MENDELEY_KEY;
    			$this->secret = KEYRING__MENDELEY_SECRET;
    		} else if ( $creds = $this->get_credentials() ) {
    			$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->requires_token( true );
    	}
    
    	function parse_response( $response ) {
    		return json_decode( $response );
    		// Get user profile information
    
    	}
    
    			function build_token_meta( $token ) {
    		// Set the token so that we can make requests using it
    		$this->set_token(
    			new Keyring_Token(
    				'linkedin',
    				new OAuthToken(
    					$token['oauth_token'],
    					$token['oauth_token_secret']
    				),
    				array(
    					'type' => 'access',
    				)
    			)
    		);
    
    		// Get user profile information
    		$response = $this->request( "http://api.mendeley.com/oapi/profiles/info/me/" );
    
    		if ( Keyring_Util::is_error( $response ) )
    			return array();
    
    		$this->person = $response;
    		$meta = array(
    			'user_id' => $this->person->id,
    			'name'    => $this->person->formattedName,
    		);
    
    		return $meta;
    	}
    
    	function get_display( Keyring_Token $token ) {
    		return $token->get_meta( 'name' );
    	}
    
    }
    
    add_action( 'keyring_load_services', array( 'Keyring_Service_Mendeley', 'init' ) );

    I can not set a new token. It just doesn't work.
    Thanks for your help.

    http://wordpress.org/extend/plugins/keyring/

  2. Beau Lebens
    Member
    Plugin Author

    Posted 1 year ago #

    Hi, it looks like this is based on the packaged LinkedIn service, because you've accidentally left in a reference to LinkedIn that's probably making this fail. I'm not sure if it's the only problem, but it definitely won't help :)

    In the build_token_meta() method, you're still referencing LinkedIn for the token type, and then also for the data being returned as meta. You'll definitely need to change the token type there to be 'mendeley' to match your service name, and you probably will need to change the data being returned.

    Hope that helps.

  3. Federicoviola
    Member
    Posted 1 year ago #

    Thanks Beau for your answer.
    But I don't think that the problem were that. I've tested this service with only this piece of code:

    class Keyring_Service_Mendeley extends Keyring_Service_OAuth1 {
    	const NAME  = 'mendeley';
    	const LABEL = 'Mendeley';
    
    	var $authorization_header;
    	var $authorization_realm;
    
    	function __construct() {
    		parent::__construct();
    
    		// Enable "basic" UI for entering key/secret
    		add_action( 'keyring_mendeley_manage_ui', array( $this, 'basic_ui' ) );
    
    		//$this->authorization_header = true;
    		$this->authorization_realm = "http://www.mendeley.com/oapi/";
    
    		$this->set_endpoint( 'request_token', 'http://www.mendeley.com/oauth/request_token/', 'GET' );
    		$this->set_endpoint( 'authorize',     'http://www.mendeley.com/oauth/authorize/', 'GET');
    		$this->set_endpoint( 'access_token',  'http://www.mendeley.com/oauth/access_token/',  'GET');
    
    		if ( defined( 'KEYRING__MENDELEY_KEY' ) && defined( 'KEYRING__MENDELEY_SECRET' ) ) {
    			$this->key = KEYRING__MENDELEY_KEY;
    			$this->secret = KEYRING__MENDELEY_SECRET;
    		} else if ( $creds = $this->get_credentials() ) {
    			$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->requires_token( true );
    	}

    And it works but returns me in WP a blank page. In the browser I get the following line:

    http://192.168.1.40/wordpress/wp-admin/tools.php?page=keyring&service=mendeley&action=verify&kr_nonce=597fcb6eff&nonce=e78d6c2116&oauth_token=a9b3e0b01c0ce48abcb5e19788e03b65050bf2092&oauth_verifier=949c5605a1

    The twitter service works and return in the browser the following line:

    http://192.168.1.40/wordpress/wp-admin/tools.php?page=keyring&service=twitter&action=created&id=59&kr_nonce=ed950fe3d3

    The &action is different. I don't realize why...
    BTW I based my code in the twitter template.

  4. Beau Lebens
    Member
    Plugin Author

    Posted 1 year ago #

    If that's the URL where it's stopping/failing, then it sounds like something in the verify_token() step is failing.

    Unfortunately the error-reporting/catching is pretty bad in Keyring at the moment, so these whitescreens can happen in some situations.

    You probably don't need the authorization_realm, I've just included that there because some services require something specific in there.

    Have you tried enabling KEYRING__DEBUG_MODE and seeing what you get in your error_log? It is very verbose, and will probably give you something to check out.

    Also, I'm just about to commit a bunch of changes to trunk which will change a bunch of things, but also hopefully make it a bit better to work with.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags