Skip to content
This repository
Browse code

Merge pull request #24 from mattwkelly/master

Added ability to extend tokens
  • Loading branch information...
commit 1270f0debaab3b93d683a9817c7adbc7e9e5cdc0 2 parents 42481fa + f336af0
Matt Kelly authored August 07, 2012

Showing 1 changed file with 43 additions and 0 deletions. Show diff stats Hide diff stats

  1. 43  src/base_facebook.php
43  src/base_facebook.php
@@ -344,6 +344,49 @@ public function setAccessToken($access_token) {
344 344
   }
345 345
 
346 346
   /**
  347
+   * Extend an access token, while removing the short-lived token that might
  348
+   * have been generated via client-side flow. Thanks to http://bit.ly/b0Pt0H
  349
+   * for the workaround.
  350
+   */
  351
+  public function setExtendedAccessToken() {
  352
+    try {
  353
+      // need to circumvent json_decode by calling _oauthRequest
  354
+      // directly, since response isn't JSON format.
  355
+      $access_token_response = $this->_oauthRequest(
  356
+        $this->getUrl('graph', '/oauth/access_token'),
  357
+        $params = array(
  358
+          'client_id' => $this->getAppId(),
  359
+          'client_secret' => $this->getAppSecret(),
  360
+          'grant_type' => 'fb_exchange_token',
  361
+          'fb_exchange_token' => $this->getAccessToken(),
  362
+        )
  363
+      );
  364
+    }
  365
+    catch (FacebookApiException $e) {
  366
+      // most likely that user very recently revoked authorization.
  367
+      // In any event, we don't have an access token, so say so.
  368
+      return false;
  369
+    }
  370
+  
  371
+    if (empty($access_token_response)) {
  372
+      return false;
  373
+    }
  374
+      
  375
+    $response_params = array();
  376
+    parse_str($access_token_response, $response_params);
  377
+    
  378
+    if (!isset($response_params['access_token'])) {
  379
+      return false;
  380
+    }
  381
+    
  382
+    $this->destroySession();
  383
+    
  384
+    $this->setPersistentData(
  385
+      'access_token', $response_params['access_token']
  386
+    );
  387
+  }
  388
+
  389
+  /**
347 390
    * Determines the access token that should be used for API calls.
348 391
    * The first time this is called, $this->accessToken is set equal
349 392
    * to either a valid user access token, or it's set to the application

0 notes on commit 1270f0d

Please sign in to comment.
Something went wrong with that request. Please try again.