Cheetah
OAuth2Client.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\Authentication;
25 
33 
40 {
44  const BASE_AUTHORIZATION_URL = 'https://www.facebook.com';
45 
51  protected $app;
52 
58  protected $client;
59 
65  protected $graphVersion;
66 
72  protected $lastRequest;
73 
80  {
81  $this->app = $app;
82  $this->client = $client;
83  $this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
84  }
85 
92  public function getLastRequest()
93  {
94  return $this->lastRequest;
95  }
96 
104  public function debugToken($accessToken)
105  {
106  $accessToken = $accessToken instanceof AccessToken ? $accessToken->getValue() : $accessToken;
107  $params = ['input_token' => $accessToken];
108 
109  $this->lastRequest = new FacebookRequest(
110  $this->app,
111  $this->app->getAccessToken(),
112  'GET',
113  '/debug_token',
114  $params,
115  null,
116  $this->graphVersion
117  );
118  $response = $this->client->sendRequest($this->lastRequest);
119  $metadata = $response->getDecodedBody();
120 
121  return new AccessTokenMetadata($metadata);
122  }
123 
135  public function getAuthorizationUrl($redirectUrl, $state, array $scope = [], array $params = [], $separator = '&')
136  {
137  $params += [
138  'client_id' => $this->app->getId(),
139  'state' => $state,
140  'response_type' => 'code',
141  'sdk' => 'php-sdk-' . Facebook::VERSION,
142  'redirect_uri' => $redirectUrl,
143  'scope' => implode(',', $scope)
144  ];
145 
146  return static::BASE_AUTHORIZATION_URL . '/' . $this->graphVersion . '/dialog/oauth?' . http_build_query($params, null, $separator);
147  }
148 
159  public function getAccessTokenFromCode($code, $redirectUri = '')
160  {
161  $params = [
162  'code' => $code,
163  'redirect_uri' => $redirectUri,
164  ];
165 
166  return $this->requestAnAccessToken($params);
167  }
168 
178  public function getLongLivedAccessToken($accessToken)
179  {
180  $accessToken = $accessToken instanceof AccessToken ? $accessToken->getValue() : $accessToken;
181  $params = [
182  'grant_type' => 'fb_exchange_token',
183  'fb_exchange_token' => $accessToken,
184  ];
185 
186  return $this->requestAnAccessToken($params);
187  }
188 
199  public function getCodeFromLongLivedAccessToken($accessToken, $redirectUri = '')
200  {
201  $params = [
202  'redirect_uri' => $redirectUri,
203  ];
204 
205  $response = $this->sendRequestWithClientParams('/oauth/client_code', $params, $accessToken);
206  $data = $response->getDecodedBody();
207 
208  if (!isset($data['code'])) {
209  throw new FacebookSDKException('Code was not returned from Graph.', 401);
210  }
211 
212  return $data['code'];
213  }
214 
224  protected function requestAnAccessToken(array $params)
225  {
226  $response = $this->sendRequestWithClientParams('/oauth/access_token', $params);
227  $data = $response->getDecodedBody();
228 
229  if (!isset($data['access_token'])) {
230  throw new FacebookSDKException('Access token was not returned from Graph.', 401);
231  }
232 
233  // Graph returns two different key names for expiration time
234  // on the same endpoint. Doh! :/
235  $expiresAt = 0;
236  if (isset($data['expires'])) {
237  // For exchanging a short lived token with a long lived token.
238  // The expiration time in seconds will be returned as "expires".
239  $expiresAt = time() + $data['expires'];
240  } elseif (isset($data['expires_in'])) {
241  // For exchanging a code for a short lived access token.
242  // The expiration time in seconds will be returned as "expires_in".
243  // See: https://developers.facebook.com/docs/facebook-login/access-tokens#long-via-code
244  $expiresAt = time() + $data['expires_in'];
245  }
246 
247  return new AccessToken($data['access_token'], $expiresAt);
248  }
249 
261  protected function sendRequestWithClientParams($endpoint, array $params, $accessToken = null)
262  {
263  $params += $this->getClientParams();
264 
265  $accessToken = $accessToken ?: $this->app->getAccessToken();
266 
267  $this->lastRequest = new FacebookRequest(
268  $this->app,
269  $accessToken,
270  'GET',
271  $endpoint,
272  $params,
273  null,
274  $this->graphVersion
275  );
276 
277  return $this->client->sendRequest($this->lastRequest);
278  }
279 
285  protected function getClientParams()
286  {
287  return [
288  'client_id' => $this->app->getId(),
289  'client_secret' => $this->app->getSecret(),
290  ];
291  }
292 }
Facebook\Authentication\OAuth2Client\getAuthorizationUrl
getAuthorizationUrl($redirectUrl, $state, array $scope=[], array $params=[], $separator='&')
Definition: OAuth2Client.php:135
Facebook\Authentication\OAuth2Client\$graphVersion
$graphVersion
Definition: OAuth2Client.php:65
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\Facebook\DEFAULT_GRAPH_VERSION
const DEFAULT_GRAPH_VERSION
Definition: Facebook.php:61
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
Facebook\Authentication\OAuth2Client\$client
$client
Definition: OAuth2Client.php:58
Facebook\Authentication\OAuth2Client\$lastRequest
$lastRequest
Definition: OAuth2Client.php:72
php
Facebook\Authentication\OAuth2Client\getLongLivedAccessToken
getLongLivedAccessToken($accessToken)
Definition: OAuth2Client.php:178
Facebook\FacebookResponse
Definition: FacebookResponse.php:36
Facebook\Authentication
Definition: AccessToken.php:24
Facebook\Authentication\AccessToken
Definition: AccessToken.php:32
Facebook\Authentication\OAuth2Client\getLastRequest
getLastRequest()
Definition: OAuth2Client.php:92
Facebook\Authentication\OAuth2Client\BASE_AUTHORIZATION_URL
const BASE_AUTHORIZATION_URL
Definition: OAuth2Client.php:44
Facebook\Authentication\OAuth2Client\$app
$app
Definition: OAuth2Client.php:51
Facebook\Exceptions\FacebookResponseException
Definition: FacebookResponseException.php:34
Facebook\Authentication\OAuth2Client\getCodeFromLongLivedAccessToken
getCodeFromLongLivedAccessToken($accessToken, $redirectUri='')
Definition: OAuth2Client.php:199
Facebook\Authentication\OAuth2Client\sendRequestWithClientParams
sendRequestWithClientParams($endpoint, array $params, $accessToken=null)
Definition: OAuth2Client.php:261
Facebook\Authentication\OAuth2Client\__construct
__construct(FacebookApp $app, FacebookClient $client, $graphVersion=null)
Definition: OAuth2Client.php:79
Facebook\FacebookApp
Definition: FacebookApp.php:30
Facebook\Authentication\OAuth2Client
Definition: OAuth2Client.php:40
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
Facebook\Authentication\AccessTokenMetadata
Definition: AccessTokenMetadata.php:37
Facebook\Authentication\OAuth2Client\debugToken
debugToken($accessToken)
Definition: OAuth2Client.php:104
Facebook\Facebook\VERSION
const VERSION
Definition: Facebook.php:56
Facebook\Authentication\OAuth2Client\getClientParams
getClientParams()
Definition: OAuth2Client.php:285
Facebook\FacebookClient
Definition: FacebookClient.php:37
Facebook\Authentication\AccessToken\getValue
getValue()
Definition: AccessToken.php:134
Facebook\Authentication\OAuth2Client\getAccessTokenFromCode
getAccessTokenFromCode($code, $redirectUri='')
Definition: OAuth2Client.php:159
Facebook\Authentication\OAuth2Client\requestAnAccessToken
requestAnAccessToken(array $params)
Definition: OAuth2Client.php:224
Facebook\Facebook
Definition: Facebook.php:52
Facebook\FacebookRequest
Definition: FacebookRequest.php:40