Linkedin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. /**
  11. * Linkedin service.
  12. *
  13. * @author Antoine Corcy <contact@sbin.dk>
  14. * @link http://developer.linkedin.com/documents/authentication
  15. */
  16. class Linkedin extends AbstractService
  17. {
  18. /**
  19. * Defined scopes
  20. * @link http://developer.linkedin.com/documents/authentication#granting
  21. */
  22. const SCOPE_R_BASICPROFILE = 'r_basicprofile';
  23. const SCOPE_R_FULLPROFILE = 'r_fullprofile';
  24. const SCOPE_R_EMAILADDRESS = 'r_emailaddress';
  25. const SCOPE_R_NETWORK = 'r_network';
  26. const SCOPE_R_CONTACTINFO = 'r_contactinfo';
  27. const SCOPE_RW_NUS = 'rw_nus';
  28. const SCOPE_RW_COMPANY_ADMIN = 'rw_company_admin';
  29. const SCOPE_RW_GROUPS = 'rw_groups';
  30. const SCOPE_W_MESSAGES = 'w_messages';
  31. const SCOPE_W_SHARE = 'w_share';
  32. public function __construct(
  33. CredentialsInterface $credentials,
  34. ClientInterface $httpClient,
  35. TokenStorageInterface $storage,
  36. $scopes = array(),
  37. UriInterface $baseApiUri = null
  38. ) {
  39. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  40. if (null === $baseApiUri) {
  41. $this->baseApiUri = new Uri('https://api.linkedin.com/v1/');
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getAuthorizationEndpoint()
  48. {
  49. return new Uri('https://www.linkedin.com/uas/oauth2/authorization');
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getAccessTokenEndpoint()
  55. {
  56. return new Uri('https://www.linkedin.com/uas/oauth2/accessToken');
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function getAuthorizationMethod()
  62. {
  63. return static::AUTHORIZATION_METHOD_QUERY_STRING_V2;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function parseAccessTokenResponse($responseBody)
  69. {
  70. $data = json_decode($responseBody, true);
  71. if (null === $data || !is_array($data)) {
  72. throw new TokenResponseException('Unable to parse response.');
  73. } elseif (isset($data['error'])) {
  74. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  75. }
  76. $token = new StdOAuth2Token();
  77. $token->setAccessToken($data['access_token']);
  78. $token->setLifeTime($data['expires_in']);
  79. if (isset($data['refresh_token'])) {
  80. $token->setRefreshToken($data['refresh_token']);
  81. unset($data['refresh_token']);
  82. }
  83. unset($data['access_token']);
  84. unset($data['expires_in']);
  85. $token->setExtraParams($data);
  86. return $token;
  87. }
  88. }