Skip to content

Update README.md #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,36 @@ $provider = new Stevenmaguire\OAuth2\Client\Provider\Salesforce([
```
For further usage of this package please refer to the [core package documentation on "Authorization Code Grant"](https://github.com/thephpleague/oauth2-client#usage).

### Refreshing a Token
### Getting and/or Refreshing a Token

```php
$provider = new Stevenmaguire\OAuth2\Client\Provider\Salesforce([
'clientId' => '{salesforce-client-id}',
'clientSecret' => '{salesforce-client-secret}',
'redirectUri' => 'https://example.com/callback-url'
'redirectUri' => 'https://example.com/callback-url',
]);

$existingAccessToken = getAccessTokenFromYourDataStore();
//Here are two options for getting the required token.

// Option 1: You have a token already saved in your local data store.
// Create a routine to pull that token from your local data storage.
$thisAccessToken = getAccessTokenFromYourDataStore();

if ($existingAccessToken->hasExpired()) {
// Option 2: You don't have a pre-saved token or that local storage call failed (e.g. empty($thisAccessToken) ).
// Use the Salesforce OAuth API to provide a token good for only this session.
if empty($thisAccessToken) {
$thisAccessToken = $provider->getAccessToken('password', ['username'=>'USERNAME', 'password' => 'PASSWORD']);
}

// Once you have a token you can check if it's expired and refresh
if ($thisAccessToken->hasExpired()) {
$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $existingAccessToken->getRefreshToken()
'refresh_token' => $thisAccessToken->getRefreshToken()
]);

// Purge old access token and store new access token to your data store.
// If using Option 1: Purge old access token and store new access token to your data store.
// If using Option 2: $thisAccessToken = $newAccessToken;

}
```

Expand Down