Access_token is getting truncated when making a graph API call inside Azure function

markhilton 0 Reputation points
2023-10-01T19:24:35.4333333+00:00

I am trying to get Azure AD users via Graph API using Azure function

First I try to get token


$response = $Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body
Write-Host "response $response.access_token"

Token I am getting printed is getting truncated and not the complete token. However from postman I am receiving the complete token.

I also tried "select-object", but even that is also providing same truncated output

$response = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body | Select-Object -ExpandProperty access_token
    Write-Host "response $response" 

Can you let me know how to get the complete token

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,214 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,579 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,693 questions
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 43,491 Reputation points
    2023-10-03T09:27:18.5333333+00:00

    Hi @markhilton

    Refer to the sample script below and it should work for you.

    #region Authentication
    #We use the client credentials flow as an example. For production use, REPLACE the code below with your preferred auth method. NEVER STORE CREDENTIALS IN PLAIN TEXT!!!
    
    #Variables to configure
    $tenantID = "tenant.onmicrosoft.com" #your tenantID or tenant root domain
    $appID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #the GUID of your app.
    $client_secret = "verylongsecurestring" #client secret for the app
    
    #Prepare token request
    $url = 'https://login.microsoftonline.com/' + $tenantId + '/oauth2/v2.0/token'
    
    $body = @{
        grant_type = "client_credentials"
        client_id = $appID
        client_secret = $client_secret
        scope = "https://graph.microsoft.com/.default"
    }
    
    #Obtain the token
    Write-Verbose "Authenticating..."
    try { $tokenRequest = Invoke-WebRequest -Method Post -Uri $url -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing -ErrorAction Stop }
    catch { Write-Host "Unable to obtain access token, aborting..."; return }
    
    $token = ($tokenRequest.Content | ConvertFrom-Json).access_token
    
    Write-Host $token
    

    Print token:

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.