none
Graph API returning HTML and not json RRS feed

  • Question

  • Hi,

    Hopefully this is the right forum for Poweshell calling the Graph API!  I'm calling the graph API with Poweshell invoke-RestMethod.  I'm doing this after I've got an OAuth auth code and Access token.  I'll leave that bit of the code out and show you the bit that isn't working.  I'm trying to pull down emails from a specific inbox as json

    Here is my code:


    #get Access Token
    $body = "grant_type=authorization_code&redirect_uri=$redirectUri&client_id=$clientId&client_secret=$clientSecretEncoded&code=$authCode&resource=$resource"
    $Authorization = Invoke-RestMethod https://login.microsoftonline.com/common/oauth2/token `
        -Method Post -ContentType "application/x-www-form-urlencoded" `
        -Body $body `
        -ErrorAction STOP
    
    Write-output $Authorization.access_token
    $accesstoken = $Authorization.access_token
    
    $authheader = "Bearer " + $accesstoken
    
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization",$authheader)
    $headers.Add("Content-type","application/json")
    
    
    #$me = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri https://graph.microsoft.com/v1.0/users/user@tennant.onmicrosoft.com/messages -Method #Get
    
    $me = Invoke-RestMethod -Headers $headers -Uri https://graph.microsoft.com/v1.0/users/user@tennant.onmicrosoft.com/messages -Method get -ContentType "application/json"
    
    			
    #get Access Token
    $body = "grant_type=authorization_code&redirect_uri=$redirectUri&client_id=$clientId&client_secret=$clientSecretEncoded&code=$authCode&resource=$resource"
    $Authorization = Invoke-RestMethod https://login.microsoftonline.com/common/oauth2/token `
        -Method Post -ContentType "application/x-www-form-urlencoded" `
        -Body $body `
        -ErrorAction STOP
    
    Write-output $Authorization.access_token
    $accesstoken = $Authorization.access_token
    
    $authheader = "Bearer " + $accesstoken
    
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization",$authheader)
    $headers.Add("Content-type","application/json")
    
    
    #$me = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri https://graph.microsoft.com/v1.0/users/user@tennantk.onmicrosoft.com/messages -Method #Get
    
    $me = Invoke-RestMethod -Headers $headers -Uri https://graph.microsoft.com/v1.0/users/userid@id.onmicrosoft.com/messages -Method get -ContentType "application/json"
    
    			
    #get Access Token
    $body = "grant_type=authorization_code&redirect_uri=$redirectUri&client_id=$clientId&client_secret=$clientSecretEncoded&code=$authCode&resource=$resource"
    $Authorization = Invoke-RestMethod https://login.microsoftonline.com/common/oauth2/token `
        -Method Post -ContentType "application/x-www-form-urlencoded" `
        -Body $body `
        -ErrorAction STOP
    
    Write-output $Authorization.access_token
    $accesstoken = $Authorization.access_token
    
    $authheader = "Bearer " + $accesstoken
    
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization",$authheader)
    $headers.Add("Content-type","application/json")
    
    
    d #Get
    
    $me = Invoke-RestMethod -Headers $headers -Uri https://graph.microsoft.com/v1.0/users/user@tennant.onmicrosoft.com/messages -Method get -ContentType "application/json"
    
    			

    The data is coming back as as a Powershell object  which I can parse with ConvertTo-json but I don't want all the HTML mark up inside.  Is it possible to just get purse json back?

    I've read here that it is possible to run an OData system query (https://docs.microsoft.com/en-us/graph/query-parameters) where you can essentiall pass some select statements in the query but I get this error :

    Invoke-RestMethod : Cannot convert 'System.Object[]' to the type 'System.Uri' required by parameter 'Uri'. Specified method is not supported.







    • Edited by skypojames Tuesday, July 16, 2019 4:29 PM
    Tuesday, July 16, 2019 4:01 PM

All replies