linkedin.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * LinkedIn service definition for Keyring. Clean implementation of OAuth1
  4. */
  5. class Keyring_Service_LinkedIn extends Keyring_Service_OAuth1 {
  6. const NAME = 'linkedin';
  7. const LABEL = 'LinkedIn';
  8. function __construct() {
  9. parent::__construct();
  10. $this->authorization_header = true;
  11. $this->authorization_realm = "api.linkedin.com";
  12. // Enable "basic" UI for entering key/secret
  13. if ( ! KEYRING__HEADLESS_MODE ) {
  14. add_action( 'keyring_linkedin_manage_ui', array( $this, 'basic_ui' ) );
  15. add_filter( 'keyring_linkedin_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  16. }
  17. $this->set_endpoint( 'request_token', 'https://api.linkedin.com/uas/oauth/requestToken', 'POST' );
  18. $this->set_endpoint( 'authorize', 'https://api.linkedin.com/uas/oauth/authorize', 'GET' );
  19. $this->set_endpoint( 'access_token', 'https://api.linkedin.com/uas/oauth/accessToken', 'GET' );
  20. $creds = $this->get_credentials();
  21. $this->app_id = $creds['app_id'];
  22. $this->key = $creds['key'];
  23. $this->secret = $creds['secret'];
  24. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  25. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  26. }
  27. function basic_ui_intro() {
  28. echo '<p>' . __( "To connect to LinkedIn, you'll first need to <a href='https://www.linkedin.com/secure/developer?newapp='>create an app</a>. A lot of the details are required, but they're not actually important to the operation of your app, since Keyring will override any important settings.", 'keyring' ) . '</p>';
  29. echo '<p>' . __( "Once you've created your app, go down to the <strong>OAuth Keys</strong> section and copy the <strong>API Key</strong> value into the <strong>API Key</strong> field below, and the <strong>Secret Key</strong> value into the <strong>API Secret</strong> field and click save (you don't need an App ID value for LinkedIn).", 'keyring' ) . '</p>';
  30. }
  31. function parse_response( $response ) {
  32. if ( '<?xml' == substr( $response, 0, 5 ) ) // Errors always come back as XML
  33. return simplexml_load_string( $response );
  34. else
  35. return json_decode( $response );
  36. }
  37. function build_token_meta( $token ) {
  38. // Set the token so that we can make requests using it
  39. $this->set_token(
  40. new Keyring_Access_Token(
  41. $this->get_name(),
  42. new OAuthToken(
  43. $token['oauth_token'],
  44. $token['oauth_token_secret']
  45. )
  46. )
  47. );
  48. // Get user profile information
  49. $response = $this->request( "https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url)?format=json" );
  50. if ( Keyring_Util::is_error( $response ) ) {
  51. $meta = array();
  52. } else {
  53. $this->person = $response;
  54. $meta = array(
  55. 'user_id' => $this->person->id,
  56. 'name' => $this->person->formattedName,
  57. 'picture' => $this->person->pictureUrl,
  58. );
  59. }
  60. return apply_filters( 'keyring_access_token_meta', $meta, 'linkedin', $token, $response, $this );
  61. }
  62. function get_display( Keyring_Access_Token $token ) {
  63. return $token->get_meta( 'name' );
  64. }
  65. function test_connection() {
  66. $res = $this->request( "https://api.linkedin.com/v1/people/~:(id,formatted-name)?format=json" );
  67. if ( !Keyring_Util::is_error( $res ) )
  68. return true;
  69. return $res;
  70. }
  71. }
  72. add_action( 'keyring_load_services', array( 'Keyring_Service_LinkedIn', 'init' ) );