Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
oauth.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Linkedin
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die();
11 
12 /**
13  * Joomla Platform class for generating Linkedin API access token.
14  *
15  * @package Joomla.Platform
16  * @subpackage Linkedin
17  *
18  * @since 13.1
19  */
21 {
22  /**
23  * @var JRegistry Options for the JLinkedinOauth object.
24  * @since 13.1
25  */
26  protected $options;
27 
28  /**
29  * Constructor.
30  *
31  * @param JRegistry $options JLinkedinOauth options object.
32  * @param JHttp $client The HTTP client object.
33  * @param JInput $input The input object
34  *
35  * @since 13.1
36  */
37  public function __construct(JRegistry $options = null, JHttp $client = null, JInput $input = null)
38  {
39  $this->options = isset($options) ? $options : new JRegistry;
40 
41  $this->options->def('accessTokenURL', 'https://www.linkedin.com/uas/oauth/accessToken');
42  $this->options->def('authenticateURL', 'https://www.linkedin.com/uas/oauth/authenticate');
43  $this->options->def('authoriseURL', 'https://www.linkedin.com/uas/oauth/authorize');
44  $this->options->def('requestTokenURL', 'https://www.linkedin.com/uas/oauth/requestToken');
45 
46  // Call the JOauthV1aclient constructor to setup the object.
47  parent::__construct($this->options, $client, $input);
48  }
49 
50  /**
51  * Method to verify if the access token is valid by making a request to an API endpoint.
52  *
53  * @return boolean Returns true if the access token is valid and false otherwise.
54  *
55  * @since 13.1
56  */
57  public function verifyCredentials()
58  {
59  $token = $this->getToken();
60 
61  // Set parameters.
62  $parameters = array(
63  'oauth_token' => $token['key']
64  );
65 
66  $data['format'] = 'json';
67 
68  // Set the API url.
69  $path = 'https://api.linkedin.com/v1/people::(~)';
70 
71  // Send the request.
72  $response = $this->oauthRequest($path, 'GET', $parameters, $data);
73 
74  // Verify response
75  if ($response->code == 200)
76  {
77  return true;
78  }
79  else
80  {
81  return false;
82  }
83  }
84 
85  /**
86  * Method to validate a response.
87  *
88  * @param string $url The request URL.
89  * @param JHttpResponse $response The response to validate.
90  *
91  * @return void
92  *
93  * @since 13.1
94  * @throws DomainException
95  */
96  public function validateResponse($url, $response)
97  {
98  if (!$code = $this->getOption('success_code'))
99  {
100  $code = 200;
101  }
102 
103  if (strpos($url, '::(~)') === false && $response->code != $code)
104  {
105  if ($error = json_decode($response->body))
106  {
107  throw new DomainException('Error code ' . $error->errorCode . ' received with message: ' . $error->message . '.');
108  }
109  else
110  {
111  throw new DomainException($response->body);
112  }
113  }
114  }
115 
116  /**
117  * Method used to set permissions.
118  *
119  * @param mixed $scope String or an array of string containing permissions.
120  *
121  * @return JLinkedinOauth This object for method chaining
122  *
123  * @see https://developer.linkedin.com/documents/authentication
124  * @since 13.1
125  */
126  public function setScope($scope)
127  {
128  $this->setOption('scope', $scope);
129 
130  return $this;
131  }
132 
133  /**
134  * Method to get the current scope
135  *
136  * @return string String or an array of string containing permissions.
137  *
138  * @since 13.1
139  */
140  public function getScope()
141  {
142  return $this->getOption('scope');
143  }
144 }