Ejemplo n.º 1
0
 /**
  * Create a SharePoint Access Token (App-only Policy)
  *
  * @param   SPSite $site  SharePoint Site
  * @param   array  $extra Extra payload values to map
  * @throws  SPBadMethodCallException|SPInvalidArgumentException
  * @return  SPAccessToken
  */
 public static function createAppOnlyPolicy(SPSite $site, array $extra = [])
 {
     $config = $site->getConfig();
     if (empty($config['secret'])) {
         throw new SPBadMethodCallException('The Secret is empty/not set');
     }
     if (empty($config['acs'])) {
         throw new SPBadMethodCallException('The Azure Access Control Service URL is empty/not set');
     }
     if (!filter_var($config['acs'], FILTER_VALIDATE_URL)) {
         throw new SPInvalidArgumentException('The Azure Access Control Service URL is invalid');
     }
     if (empty($config['client_id'])) {
         throw new SPBadMethodCallException('The Client ID is empty/not set');
     }
     if (empty($config['resource'])) {
         throw new SPBadMethodCallException('The Resource is empty/not set');
     }
     $json = $site->request($config['acs'], ['headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], 'body' => http_build_query(['grant_type' => 'client_credentials', 'client_id' => $config['client_id'], 'client_secret' => $config['secret'], 'resource' => $config['resource']])], 'POST');
     return new static($json, $extra);
 }