runkeeper.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Runkeeper service definition for Keyring.
  4. * http://developer.runkeeper.com/
  5. */
  6. class Keyring_Service_RunKeeper extends Keyring_Service_OAuth2 {
  7. const NAME = 'runkeeper';
  8. const LABEL = 'RunKeeper';
  9. function __construct() {
  10. parent::__construct();
  11. // Enable "basic" UI for entering key/secret
  12. if ( ! KEYRING__HEADLESS_MODE ) {
  13. add_action( 'keyring_runkeeper_manage_ui', array( $this, 'basic_ui' ) );
  14. add_filter( 'keyring_runkeeper_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  15. }
  16. $this->set_endpoint( 'authorize', 'https://runkeeper.com/apps/authorize', 'GET' );
  17. $this->set_endpoint( 'access_token', 'https://runkeeper.com/apps/token', 'POST' );
  18. $this->set_endpoint( 'deauthorize', 'https://runkeeper.com/apps/de-authorize', 'POST' );
  19. $this->set_endpoint( 'user', 'https://api.runkeeper.com/user', 'GET' );
  20. $this->set_endpoint( 'profile', 'https://api.runkeeper.com/profile', 'GET' );
  21. $creds = $this->get_credentials();
  22. $this->app_id = $creds['app_id'];
  23. $this->key = $creds['key'];
  24. $this->secret = $creds['secret'];
  25. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  26. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  27. $this->authorization_header = 'Bearer';
  28. $this->authorization_parameter = false;
  29. }
  30. function basic_ui_intro() {
  31. echo '<p>' . sprintf( __( 'You\'ll need to <a href="%s">register a new application</a> on RunKeeper so that you can connect. Be sure to check the <strong>Read Health Information</strong> option under <strong>Permissions Requests</strong> (and explain why you want to read that data). You will also be required to set an <strong>Estimated Date of Publication</strong>.', 'keyring' ), 'http://runkeeper.com/partner/applications/register' ) . '</p>';
  32. echo '<p>' . __( "Once you've registered your application, click the <strong>Application Keys and URLs</strong> next to it, and copy the <strong>Client ID</strong> into the <strong>API Key</strong> field below, and the <strong>Client Secret</strong> value into <strong>API Secret</strong>.", 'keyring' ) . '</p>';
  33. }
  34. function build_token_meta( $token ) {
  35. $this->set_token(
  36. new Keyring_Access_Token(
  37. $this->get_name(),
  38. $token['access_token'],
  39. array()
  40. )
  41. );
  42. $response = $this->request( $this->user_url, array( 'method' => $this->user_method ) );
  43. if ( Keyring_Util::is_error( $response ) ) {
  44. $meta = array();
  45. } else {
  46. // Only useful thing in that request is userID
  47. $meta = array( 'user_id' => (int) $response->userID );
  48. // Now get the rest of their profile
  49. $profile = $this->request( $this->profile_url, array( 'method' => $this->profile_method ) );
  50. if ( !Keyring_Util::is_error( $profile ) ) {
  51. $meta['username'] = substr( $profile->profile, strrpos( $profile->profile, '/' ) + 1 );
  52. $meta['name'] = $profile->name;
  53. $meta['picture'] = $profile->large_picture;
  54. }
  55. return apply_filters( 'keyring_access_token_meta', $meta, 'runkeeper', $token, $profile, $this );
  56. }
  57. return array();
  58. }
  59. function get_display( Keyring_Access_Token $token ) {
  60. return $token->get_meta( 'name' );;
  61. }
  62. }
  63. add_action( 'keyring_load_services', array( 'Keyring_Service_RunKeeper', 'init' ) );