linkedin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/authenticate', '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. add_filter( 'keyring_linkedin_request_scope', array( $this, 'member_permissions' ) );
  27. }
  28. function basic_ui_intro() {
  29. echo '<p>' . sprintf( __( "To connect to LinkedIn, you'll first need to <a href='%s'>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' ), 'https://www.linkedin.com/secure/developer?newapp=' ) . '</p>';
  30. 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>';
  31. }
  32. function parse_response( $response ) {
  33. if ( '<?xml' == substr( $response, 0, 5 ) ) // Errors always come back as XML
  34. return simplexml_load_string( $response );
  35. else
  36. return json_decode( $response );
  37. }
  38. function member_permissions( $permissions = '' ) {
  39. $permissions = 'rw_nus+r_basicprofile';
  40. return $permissions;
  41. }
  42. function build_token_meta( $token ) {
  43. // Set the token so that we can make requests using it
  44. $this->set_token(
  45. new Keyring_Access_Token(
  46. $this->get_name(),
  47. new OAuthToken(
  48. $token['oauth_token'],
  49. $token['oauth_token_secret']
  50. )
  51. )
  52. );
  53. // Get user profile information
  54. $response = $this->request( "https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url)?format=json" );
  55. if ( Keyring_Util::is_error( $response ) ) {
  56. $meta = array();
  57. } else {
  58. $this->person = $response;
  59. $meta = array(
  60. 'user_id' => $this->person->id,
  61. 'name' => $this->person->formattedName,
  62. 'picture' => $this->person->pictureUrl,
  63. );
  64. }
  65. return apply_filters( 'keyring_access_token_meta', $meta, 'linkedin', $token, $response, $this );
  66. }
  67. function get_display( Keyring_Access_Token $token ) {
  68. return $token->get_meta( 'name' );
  69. }
  70. function test_connection() {
  71. $res = $this->request( "https://api.linkedin.com/v1/people/~:(id,formatted-name)?format=json" );
  72. if ( !Keyring_Util::is_error( $res ) )
  73. return true;
  74. return $res;
  75. }
  76. }
  77. add_action( 'keyring_load_services', array( 'Keyring_Service_LinkedIn', 'init' ) );