This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The resource URI | |
$resource = "https://graph.microsoft.com" | |
# Your Client ID and Client Secret obainted when registering your WebApp | |
$clientid = "726ec7c7-ff9e-4c28-b864-************" | |
$clientSecret = "KjYwt9tIf13kdBub7vaBJv4********************=" | |
$redirectUri = "https://localhost:8001" | |
# UrlEncode the ClientID and ClientSecret and URL's for special characters | |
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($clientid) | |
$clientSecretEncoded = [System.Web.HttpUtility]::UrlEncode($clientSecret) | |
$redirectUriEncoded = [System.Web.HttpUtility]::UrlEncode($redirectUri) | |
$resourceEncoded = [System.Web.HttpUtility]::UrlEncode($resource) | |
$scopeEncoded = [System.Web.HttpUtility]::UrlEncode("https://outlook.office.com/user.readwrite.all") | |
# Function to popup Auth Dialog Windows Form | |
Function Get-AuthCode { | |
Add-Type -AssemblyName System.Windows.Forms | |
$form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=440;Height=640} | |
$web = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{Width=420;Height=600;Url=($url -f ($Scope -join "%20")) } | |
$DocComp = { | |
$Global:uri = $web.Url.AbsoluteUri | |
if ($Global:uri -match "error=[^&]*|code=[^&]*") {$form.Close() } | |
} | |
$web.ScriptErrorsSuppressed = $true | |
$web.Add_DocumentCompleted($DocComp) | |
$form.Controls.Add($web) | |
$form.Add_Shown({$form.Activate()}) | |
$form.ShowDialog() | Out-Null | |
$queryOutput = [System.Web.HttpUtility]::ParseQueryString($web.Url.Query) | |
$output = @{} | |
foreach($key in $queryOutput.Keys){ | |
$output["$key"] = $queryOutput[$key] | |
} | |
$output | |
} | |
# Get AuthCode | |
$url = "https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&redirect_uri=$redirectUriEncoded&client_id=$clientID&resource=$resourceEncoded&prompt=admin_consent&scope=$scopeEncoded" | |
Get-AuthCode | |
# Extract Access token from the returned URI | |
$regex = '(?<=code=)(.*)(?=&)' | |
$authCode = ($uri | Select-string -pattern $regex).Matches[0].Value | |
Write-output "Received an authCode, $authCode" |
Hi
I use localhost:8000 as my app adress in azure,
Got error AADSTS90102: 'redirect_uri' value must be a valid absolute Uri.
Any ideas?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Darren,
I got the error message:
Unable to find type [System.Web.HttpUtility].
After adding "Add-Type -AssemblyName System.Web" at the beginning of the PowerShell script is started to work.
Running Windows 10 Creators update + .net 4.7.