In order to work with SharePoint Lists, Folders, Items, Files or Users, an Access Token is needed.
Access Tokens can have two authorization policies: App-only Policy and User-only Policy
There are two ways to create a new App-only Policy SPAccessToken
instance.
<?php
require 'vendor/autoload.php';
use WeAreArchitect\SharePoint\SPAccessToken;
use WeAreArchitect\SharePoint\SPException;
use WeAreArchitect\SharePoint\SPSite;
try {
// SharePoint Site settings
$settings = [
// ...
];
$site = SPSite::create('https://example.sharepoint.com/sites/mySite/', $settings);
$site->createSPAccessToken();
$token = $site->getSPAccessToken();
} catch (SPException $e) {
// handle exceptions
}
<?php
require 'vendor/autoload.php';
use WeAreArchitect\SharePoint\SPAccessToken;
use WeAreArchitect\SharePoint\SPException;
use WeAreArchitect\SharePoint\SPSite;
try {
// SharePoint Site settings
$settings = [
// ...
];
$site = SPSite::create('https://example.sharepoint.com/sites/mySite/', $settings);
$token = SPAccessToken::createAOP($site);
$site->setSPAccessToken($token);
} catch (SPException $e) {
// handle exceptions
}
Like with the App-only Policy SPAccessToken
, there's also two ways to instantiate a User-only Policy SPAccessToken
.
<?php
require 'vendor/autoload.php';
use WeAreArchitect\SharePoint\SPAccessToken;
use WeAreArchitect\SharePoint\SPException;
use WeAreArchitect\SharePoint\SPSite;
try {
// SharePoint Site settings
$settings = [
// ...
];
$site = SPSite::create('https://example.sharepoint.com/sites/mySite/', $settings);
$context_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyNTQyNGR...';
$site->createSPAccessToken($context_token);
$token = $site->getSPAccessToken();
} catch (SPException $e) {
// handle exceptions
}
<?php
require 'vendor/autoload.php';
use WeAreArchitect\SharePoint\SPAccessToken;
use WeAreArchitect\SharePoint\SPException;
use WeAreArchitect\SharePoint\SPSite;
try {
// SharePoint Site settings
$settings = [
// ...
];
$site = SPSite::create('https://example.sharepoint.com/sites/mySite/', $settings);
$context_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyNTQyNGR...';
$token = SPAccessToken::createUOP($site, $context_token);
$site->setSPAccessToken($token);
} catch (SPException $e) {
// handle exceptions
}
Note: On both User-only Policy examples above, the context token comes from the SPAppToken
HTTP POST field when the SharePoint application launches.
Retrieve an array
representation of the SPAccessToken
object.
var_dump($token->toArray());
// array(3) {
// ["token"]=>
// string(1132) "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVG..."
// ["expires"]=>
// object(Carbon\Carbon)#28 (3) {
// ["date"]=>
// string(26) "2000-01-01 00:00:00.000000"
// ["timezone_type"]=>
// int(3)
// ["timezone"]=>
// string(13) "Europe/London"
// }
// ["extra"]=>
// array(0) {
// }
// }
Check if the SPAccessToken
has expired.
if ($token->hasExpired()) {
// it's time to get a fresh token
} else {
// we're good
}
Get the expiration date of a SPAccessToken
in the form of a Carbon
object.
$carbon = $token->expireDate();
echo $carbon->diffForHumans(); // 12 hours from now
The SPAccessToken
class implements the __toString
magic method, which enables us to get the token value when we treat the object as a string
.
echo $token; // eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1uQ19WWmNBVG...
The SPAccessToken
class implements the Serializable
interface.
This allows saving the token to use at a later time, avoiding new token requests to the SharePoint API each time something needs doing.
// serialize the token
$serialized = serialize($token);
// store it in a database
// when needed, get it back
// unserialize the data
$oldToken = unserialize($serialized);
// check if it's still valid
if ($oldToken->hasExpired()) {
// request a new token from the API
}
// do something