diff --git a/README.md b/README.md index 52cc6c1..a5ceaaa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,14 @@ +### Attention! + +This a fork from the original code from [Zoonman](https://github.com/zoonman/linkedin-api-php-client), so almost all the credit for this library goes for him. + +I just made a single change in order to adapt the code for avoid an error pusblishing a new post, because Linkedin has migrated the api to the V2 Version. + +I still have to addapt some other changes (like adding new scopes for the V2 version), but I thought it would be better to publish a new package, so it can be used with composer without facing the commented error. + +Most of the examples from the original Readme are deprecated, and they should be fixed too. + + LinkedIn API Client with OAuth 2 authorization written on PHP ============================================================ [![Build Status](https://travis-ci.org/zoonman/linkedin-api-php-client.svg?branch=master)](https://travis-ci.org/zoonman/linkedin-api-php-client) [![Code Climate](https://codeclimate.com/github/zoonman/linkedin-api-php-client/badges/gpa.svg)](https://codeclimate.com/github/zoonman/linkedin-api-php-client) [![Packagist](https://img.shields.io/packagist/dt/zoonman/linkedin-api-php-client.svg)](https://packagist.org/packages/zoonman/linkedin-api-php-client) [![GitHub license](https://img.shields.io/github/license/zoonman/linkedin-api-php-client.svg)](https://github.com/zoonman/linkedin-api-php-client/blob/master/LICENSE.md) @@ -31,6 +42,7 @@ get application client id and secret. Go to [LinkedIn Developers portal](https://developer.linkedin.com/) and create new application in section My Apps. +All the documentation about using the api now resides in this [documentation](https://docs.microsoft.com/en-us/linkedin/marketing/index) #### Bootstrapping autoloader and instantiating a client diff --git a/composer.json b/composer.json index 41eebdf..7a725ca 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "zoonman/linkedin-api-php-client", + "name": "samoritano/linkedin-api-php-client-v2", "description": "LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.", "type": "library", "keywords": [ @@ -9,7 +9,7 @@ "social", "rest", "api", "client", "social network", "auth", "authorization", "wrapper", "integration", "platform" ], - "homepage": "https://github.com/zoonman/linkedin-api-php-client", + "homepage": "https://github.com/samoritano/linkedin-api-php-client-v2", "require": { "php": ">=5.6", "ext-curl": "*", @@ -33,13 +33,16 @@ "name": "Daniel J. Post", "homepage": "http://danieljpost.info/", "role": "Developer" + }, + { + "name": "Samori Bokoko", + "role": "Developer" } ], "support": { - "docs": "https://www.zoonman.com/projects/linkedin-client/", - "source": "https://github.com/zoonman/linkedin-api-php-client", - "issues": "https://github.com/zoonman/linkedin-api-php-client/issues", - "wiki": "https://github.com/zoonman/linkedin-api-php-client/wiki" + "source": "https://github.com/samoritano/linkedin-api-php-client-v2", + "issues": "https://github.com/samoritano/linkedin-api-php-client-v2/issues", + "wiki": "https://github.com/samoritano/linkedin-api-php-client-v2/wiki" }, "autoload": { "psr-4": {"LinkedIn\\": "src/"} diff --git a/src/AccessToken.php b/src/AccessToken.php index 2d0497d..2a4eacf 100644 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -45,27 +45,34 @@ class AccessToken implements \JsonSerializable protected $expiresAt; /** - * AccessToken constructor. + * Get token string * - * @param string $token - * @param int $expiresAt + * @return string */ - public function __construct($token = '', $expiresAt = 0) - { - $this->setToken($token); - $this->setExpiresAt($expiresAt); - } /** - * Get token string - * - * @return string + * @var string + */ + + protected $refreshToken; + + /** + * @var int */ + + protected $refreshTokenExpiresAt; + + public function getToken() { return $this->token; } + public function getRefreshToken() + { + return $this->refreshToken; + } + /** * Set token string * @@ -79,6 +86,19 @@ public function setToken($token) return $this; } + /** + * Set refresh token string + * + * @param string $refreshToken + * + * @return AccessToken + */ + public function setRefreshToken($refreshToken) + { + $this->refreshToken = $refreshToken; + return $this; + } + /** * The number of seconds remaining, from the time it was requested, before the token will expire. * @@ -89,6 +109,21 @@ public function getExpiresIn() return $this->expiresAt - time(); } + /** + * AccessToken constructor. + * + * @param string $token + * @param string $token + * @param int $expiresAt + */ + public function __construct($token = '', $expiresAt = 0,$refreshToken = '', $refreshTokenExpiresAt = 0 ) + { + $this->setToken($token); + $this->setExpiresAt($expiresAt); + $this->setRefreshToken($refreshToken); + $this->setRefreshTokenExpiresAt($refreshTokenExpiresAt); + } + /** * Set token expiration time * @@ -122,6 +157,16 @@ public function getExpiresAt() return $this->expiresAt; } + /** + * Get Unix epoch time when refresh token will expire + * + * @return int + */ + public function getRefreshTokenExpiresAt() + { + return $this->refreshTokenExpiresAt; + } + /** * Set Unix epoch time when token will expire * @@ -132,9 +177,21 @@ public function getExpiresAt() public function setExpiresAt($expiresAt) { $this->expiresAt = $expiresAt; - return $this; } + /** + * Set Unix epoch time when token will expire + * + * @param int $refreshTokenExpiresAt seconds, unix time + * + + */ + public function setRefreshTokenExpiresAt($refreshTokenExpiresAt) + { + $this->refreshTokenExpiresAt = $refreshTokenExpiresAt; + } + + /** * Convert API response into AccessToken * @@ -173,9 +230,21 @@ public static function fromResponseArray($responseArray) 'Access token expiration date is not specified' ); } + if (!isset($responseArray['refresh_token'])) { + throw new \InvalidArgumentException( + 'Refresh token is not available' + ); + } + if (!isset($responseArray['refresh_token_expires_in'])) { + throw new \InvalidArgumentException( + 'Refresh token expiration date is not specified' + ); + } return new static( $responseArray['access_token'], - $responseArray['expires_in'] + time() + $responseArray['expires_in'] + time(), + $responseArray['refresh_token'], + $responseArray['refresh_token_expires_in'] + time() ); } @@ -187,6 +256,8 @@ public function jsonSerialize() return [ 'token' => $this->getToken(), 'expiresAt' => $this->getExpiresAt(), + 'refreshToken' => $this->getRefreshToken(), + 'refreshTokenExpiresAt' => $this->getRefreshTokenExpiresAt() ]; } } diff --git a/src/Client.php b/src/Client.php index afdd9b3..f5c1043 100644 --- a/src/Client.php +++ b/src/Client.php @@ -308,10 +308,18 @@ public function getAccessToken($code = '') */ public static function responseToArray($response) { - return \GuzzleHttp\json_decode( - $response->getBody()->getContents(), - true - ); + if ($contents = $response->getBody()->getContents()) { + return \GuzzleHttp\json_decode( + $contents, + true + ); + } + + if ($contents = $response->getHeaders()) { + return $contents; + } + + return []; } /**