Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The Project REST Basic Operations demonstrates how to create and update a projec
To use this Project Online REST code sample, you need the following:
* An Office 365 tenant with a Project license
* PowerShell v4.0
* Latest ADAL.NET library. It is available as a Nuget Package from [here](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/)
* Latest MSAL .Net library. It is available as a Nuget Package from [here](https://www.nuget.org/packages/Microsoft.Identity.Client/)
* For the files downloaded, please run "Unblock-File *" to unblock accessing the file.

###Modules
Expand Down
31 changes: 18 additions & 13 deletions ReST.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,36 @@ Set-StrictMode -Version "Latest" # http://technet.microsoft.com/en-us/library/dd
$global:accessHeader = ''
$global:digestValue = ''

[Reflection.Assembly]::LoadFrom("$($PSScriptRoot)\Microsoft.IdentityModel.Clients.ActiveDirectory.dll") | Out-Null
[Reflection.Assembly]::LoadFrom("$($PSScriptRoot)\Microsoft.Identity.Client.dll") | Out-Null

function Get-AADAuthToken([Uri] $Uri)
function Get-AuthToken([Uri] $Uri)
{
# NOTE: Create an azure app and update $clientId and $redirectUri below
# NOTE: Create an azure app and update $clientId,$tenantId and $redirectUri below
$clientId = ""
$redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
$tenantId = ""
$redirectUri = ""
# user's PJO login account
$user = ""
$scopes = New-Object System.Collections.Generic.List[string]
# Project.Write Permission scope for app eg:"https://contoso.sharepoint.com/Project.Write"
$writeScope = ""
$scopes.Add($writeScope)
$pcaConfig = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($clientId).WithTenantId($tenantId).WithRedirectUri($redirectUri);

$authority = "https://login.microsoftonline.com/common"
$resource = $Uri.GetLeftPart([System.UriPartial]::Authority);

$promptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always
$platformParam = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList $promptBehavior
$authenticationContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority, $False
$authenticationResult = $authenticationContext.AcquireTokenAsync($resource, $clientId, $redirectUri, $platformParam).Result
$authenticationResult = $pcaConfig.Build().AcquireTokenInteractive($scopes).WithPrompt([Microsoft.Identity.Client.Prompt]::NoPrompt).WithLoginHint($user).ExecuteAsync().Result;

return $authenticationResult
}

# Gets Auth Token using MSAL library.
function Set-SPOAuthenticationTicket([string] $siteUrl)
{
$siteUri = New-Object Uri -ArgumentList $siteUrl

$authResult = Get-AADAuthToken -Uri $siteUri
$authResult = Get-AuthToken -Uri $siteUri
if ($authResult -ne $null)
{
$global:accessHeader = $authResult.AccessTokenType + " " + $authResult.AccessToken
$global:accessHeader = $authResult.CreateAuthorizationHeader()
}

if ([String]::IsNullOrEmpty($global:accessHeader))
Expand Down Expand Up @@ -67,6 +70,8 @@ function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method

# set Authorization header
$req.Headers.Add("Authorization", $global:accessHeader)
# handle ETag
$req.Headers.Add("If-Match", "*")

if (-not $isDigestRequest)
{
Expand Down