Skip to content

Commit

Permalink
Add support of uploading media
Browse files Browse the repository at this point in the history
Returns "Not enough permissions to access media resource".
I hope LinkedIn will resolve it eventually.
  • Loading branch information
zoonman committed Feb 9, 2018
1 parent f212b6d commit a18e451
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ Change default API root
$client->setApiRoot('https://api.linkedin.com/v2/');
```

Try to upload image to LinkedIn. See [Rich Media Shares]()https://developer.linkedin.com/docs/guide/v2/shares/rich-media-shares#upload)
(returns "Not enough permissions to access media resource" for me).
I assume you have to be LinkedIn partner or something like that.

```php
$filename = '/path/to/image.jpg';
$client->setApiRoot('https://api.linkedin.com/');
$mp = $client->upload($filename);
```

## Contributing

Please, open PR with your changes linked to an GitHub issue.
Expand Down
Binary file added examples/demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@
);
pp($companyShare);

/*
// Returns {"serviceErrorCode":100,"message":"Not enough permissions to access media resource","status":403}
// You have to be whitelisted or so by LinkedIn
$filename = './demo.jpg';
$client->setApiRoot('https://api.linkedin.com/');
$mp = $client->upload($filename);
*/
} catch (\LinkedIn\Exception $exception) {
// in case of failure, provide with details
pp($exception);
Expand Down
37 changes: 37 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public function getAccessToken($code = '')
'headers' => [
'Content-Type' => 'application/json',
'x-li-format' => 'json',
'Connection' => 'Keep-Alive'
],
]);
try {
Expand Down Expand Up @@ -532,6 +533,42 @@ public function post($endpoint, array $params = [])
return $this->api($endpoint, $params, Method::POST);
}

/**
* @param $path
* @return array
* @throws Exception
*/
public function upload($path)
{
$headers = $this->getApiHeaders();
unset($headers['Content-Type']);
//$headers = [];
if ($this->isUsingTokenParam()) {
//
} else {
$headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken();
}
$guzzle = new GuzzleClient([
'base_uri' => $this->getApiRoot()
]);
$options = [
'multipart' => [
[
'name' => basename($path),
'filename' => basename($path),
'contents' => fopen($path, 'r')
]
],
'headers' => $headers,
];
try {
$response = $guzzle->request(Method::POST, 'media/upload', $options);
} catch (RequestException $requestException) {
throw Exception::fromRequestException($requestException);
}
return self::responseToArray($response);
}

/**
* @param array $params
* @param string $method
Expand Down

0 comments on commit a18e451

Please sign in to comment.