<commit_before><#
.SYNOPSIS
test graph rest script 

.NOTES
    https://myapps.microsoft.com/ <--- can only be accessed from 'work' account
    have to set 'api permissions' on app registration. add 'microsoft graph' 'application permissions'
    https://docs.microsoft.com/en-us/graph/auth-v2-user
    https://login.microsoftonline.com/common/adminconsent?client_id={client-id}
    https://login.microsoftonline.com/common/adminconsent?client_id=
    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow
    https://login.microsoftonline.com/common/adminconsent?client_id=6731de76-14a6-49ae-97bc-6eba6914391e&state=12345&redirect_uri=http://localhost/myapp/permissions
    https://stackoverflow.com/questions/66106927/webview2-in-powershell-winform-gui
    BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
    https://docs.microsoft.com/en-us/azure/active-directory/develop/sample-v2-code

    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc
    schema:
    https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration

.LINK
    [net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
    invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/graph-api-rest.ps1" -outFile "$pwd\graph-api-rest.ps1";
    .\graph-api-rest.ps1
#>
[cmdletbinding()]
param(
    $tenantId = 'common',
    [ValidateSet('v1.0', 'beta')]
    $apiVersion = 'beta', #'v1.0'
    $graphApiUrl = "https://graph.microsoft.com/$apiVersion/$tenantId/",
    $query = 'applications', #'$metadata#applications',
    $clientId = '14d82eec-204b-4c2f-b7e8-296a70dab67e', # well-known ps aad client id generated on connect
    $clientSecret,
    [ValidateSet("get", "post")]
    $method = "get",
    [ValidateSet('application/x-www-form-urlencoded', 'application/json', 'application/xml', '*/*')]
    $contentType = 'application/json', # '*/*',
    $body = @{},
    $headers = @{'accept' = $contentType },
    $scope = 'user.read openid profile Application.Read.All', # 'https://graph.microsoft.com/.default', #'Application.Read.All offline_access user.read mail.read',
    [ValidateSet('urn:ietf:params:oauth:grant-type:device_code', 'client_credentials', 'authorization_code')]
    $grantType = 'urn:ietf:params:oauth:grant-type:device_code', #'client_credentials', #'authorization_code'
    $redirectUrl = 'http://localhost',
    [switch]$force
)

$global:logonResult = $null

function main() {
    if (!$clientId -and !$clientSecret) {
        write-error '$clientid and $clientSecret need to be specified'
        return
    }

    if (!$global:accessToken -or ($global:accessTokenExpiration -lt (get-date)) -or $force) {
        get-restAuth
        get-restToken
    }

    call-graphQuery
    write-host "use: `$global:restQuery"
}

function get-restAuth() {
    # requires app registration api permissions with 'devops' added
    # so cannot use internally
    write-host "auth request"<commit_msg>add Application.ReadWrite.All scope<commit_after><#
.SYNOPSIS
test graph rest script 

.NOTES
    https://myapps.microsoft.com/ <--- can only be accessed from 'work' account
    have to set 'api permissions' on app registration. add 'microsoft graph' 'application permissions'
    https://docs.microsoft.com/en-us/graph/auth-v2-user
    https://login.microsoftonline.com/common/adminconsent?client_id={client-id}
    https://login.microsoftonline.com/common/adminconsent?client_id=
    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow
    https://login.microsoftonline.com/common/adminconsent?client_id=6731de76-14a6-49ae-97bc-6eba6914391e&state=12345&redirect_uri=http://localhost/myapp/permissions
    https://stackoverflow.com/questions/66106927/webview2-in-powershell-winform-gui
    BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
    https://docs.microsoft.com/en-us/azure/active-directory/develop/sample-v2-code

    https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc
    schema:
    https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration

.LINK
    [net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
    invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/graph-api-rest.ps1" -outFile "$pwd\graph-api-rest.ps1";
    .\graph-api-rest.ps1
#>
[cmdletbinding()]
param(
    $tenantId = 'common',
    [ValidateSet('v1.0', 'beta')]
    $apiVersion = 'beta', #'v1.0'
    $graphApiUrl = "https://graph.microsoft.com/$apiVersion/$tenantId/",
    $query = 'applications', #'$metadata#applications',
    $clientId = '14d82eec-204b-4c2f-b7e8-296a70dab67e', # well-known ps aad client id generated on connect
    $clientSecret,
    [ValidateSet("get", "post")]
    $method = "get",
    [ValidateSet('application/x-www-form-urlencoded', 'application/json', 'application/xml', '*/*')]
    $contentType = 'application/json', # '*/*',
    $body = @{},
    $headers = @{'accept' = $contentType },
    $scope = 'user.read openid profile Application.ReadWrite.All', # 'https://graph.microsoft.com/.default', #'Application.Read.All offline_access user.read mail.read',
    [ValidateSet('urn:ietf:params:oauth:grant-type:device_code', 'client_credentials', 'authorization_code')]
    $grantType = 'urn:ietf:params:oauth:grant-type:device_code', #'client_credentials', #'authorization_code'
    #$redirectUrl = 'http://localhost',
    [switch]$force
)

$global:logonResult = $null

function main() {
    if (!$clientId -and !$clientSecret) {
        write-error '$clientid and $clientSecret need to be specified'
        return
    }

    if (!$global:accessToken -or ($global:accessTokenExpiration -lt (get-date)) -or $force) {
        get-restAuth
        get-restToken
    }

    call-graphQuery
    write-host "use: `$global:restQuery"
}

function get-restAuth() {
    # requires app registration api permissions with 'devops' added
    # so cannot use internally
    write-host "auth request"
{'fraction_non_alphanumeric': 0.15639735932096824, 'fraction_numerical': 0.024834957560515562, 'mean_word_length': 4.177379983726607, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 28, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351311', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2117, 'n_words': 313}