Skip to content

Commit

Permalink
Added ability to call public api methods (without access token) (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodge committed Jul 14, 2014
1 parent 450dc00 commit 481ebad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Yii2 EAuth Change Log
=====================

### 2.2.3 (15.07.2014)
* Added ability to call public api methods (without access token) (#28)

### 2.2.2 (15.07.2014)
* Fixed wrong redirect_uri when popup is used

Expand Down
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,43 @@ You can extend base classes with necessary methods and then write something like
<?php
/** @var $eauth EAuthServiceBase */
$eauth = Yii::$app->eauth->getIdentity('facebook');

// to get protected resources user should be authenticated:
if ($eauth->getIsAuthenticated()) {
$eauth->callApiMethod();
$eauth->callAnotherApiMethod();
$eauth->callProtectedApiMethod();
$eauth->callAnotherProtectedApiMethod();
}

// or you can get public resources at any time:
$eauth->callPublicApiMethod();
$eauth->callAnotherPublicApiMethod();
```

Example of a API call method:

```php
<?php
class FacebookOAuth2Service extends \nodge\eauth\services\FacebookOAuth2Service
{
public function fooApiMethod($bar) {
$api_method = 'me'; // ex. for Facebook this results to https://graph.facebook.com/me

// get protected resource
$response = $this->makeSignedRequest($api_method, [
'query' => [ 'foo' => 'bar' ], // GET arguments
'data' => [ 'foo' => 'bar' ], // POST arguments
'headers' => [ 'X-Foo' => 'bar' ], // Extra HTTP headers
]);

// you can get public resources with the same API:
//$response = $this->makeRequest($api_method, $options);

// process $response
$data = process($response);

// return results
return $data;
}
}
```

Expand Down
35 changes: 34 additions & 1 deletion src/oauth/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function getTokenDefaultLifetime()
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
* @throws ErrorException
Expand Down Expand Up @@ -194,6 +194,39 @@ public function makeSignedRequest($url, $options = array(), $parseResponse = tru
return $response;
}

/**
* Returns the public resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
*/
public function makeRequest($url, $options = array(), $parseResponse = true) {
if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}

$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}

$data = isset($options['data']) ? $options['data'] : array();
$method = !empty($data) ? 'POST' : 'GET';

$headers = isset($options['headers']) ? $options['headers'] : array();
$headers = array_merge($this->getProxy()->getExtraApiHeaders(), $headers);

$response = $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method);
if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}

return $response;
}

/**
* Parse response and check for errors.
Expand Down

0 comments on commit 481ebad

Please sign in to comment.