
Function fONGetSections()
'============================================================================
' Name        : fONGetSections
' Author      : Erica L Ingram
' Copyright   : 2019, A Quo Co.
' Call command: Call fONGetSections()
' Description : get ON sections
'blog post explanation at https://transcription.aquoco.co/2019/05/08/using-vba-with-json-onenote/
'sample database at https://www.aquoco.co/database3.zip
'============================================================================
Dim sURL As String, sToken As String, sLine4 As String
Dim Parsed As Dictionary
Dim sFile1 As String, sFile2 As String, sFile3 As String, sFile4 As String
Dim sLine1 As String, sLine2 As String, sLine3 As String
Dim sResponseText As String, apiWaxLRS As String
Dim sONID As String, sSelfLink As String, sCreatedDateTime As String
Dim sDisplayName As String, sLastModifiedDateTime As String, isDefault As String
Dim sUserRole As String, sIsShared As String, sSectionsUrl As String, sSectionGroupsUrl As String
Dim sCBDisplayName As String, sCBLastModifiedName As String
Dim sCBLWebURL As String, sIsDefault As String
Dim rep, vDetails, rep1, rep2, rep3, rep4, rep5, rep6, rep7, rep8
Dim sJSONResponse As String, sCBID As String
Dim sCBLastModifiedID As String, sCBLClientURL As String
Dim sPNID As String, sPNDisplayName As String
Dim sPNSelf As String, sPSGODContext As String, sPSG As String
Dim sNotebookID As String, sState As String


'before auth if you make privilege changes you must visit this website and accept permissions.
'sState = ""
'"https://login.microsoftonline.com/" & sLine2 & "adminconsent?client_id=" & sLine3 & "&state=" & sState & "&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient"
'

sFile1 = "C:\other\6.txt" 'object id / user ID
sFile2 = "C:\other\7.txt" 'directory tenant id
sFile3 = "C:\other\8.txt" 'application client id
sFile4 = "C:\other\9.txt" 'secret

Open sFile1 For Input As #1
Line Input #1, sLine1
Close #1

Open sFile2 For Input As #2
Line Input #2, sLine2
Close #2

Open sFile3 For Input As #3
Line Input #3, sLine3
Close #3

Open sFile4 For Input As #4
Line Input #4, sLine4
Close #4

sURL = "https://login.microsoftonline.com/" & sLine2 & "/oauth2/v2.0/token"

With CreateObject("WinHttp.WinHttpRequest.5.1")
    .Open "POST", sURL, False
    .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    .setRequestHeader "Host", "login.microsoftonline.com"
    .send ("client_id=" & sLine3 & "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=" & sLine4 & "&grant_type=client_credentials")
    .waitForResponse
    apiWaxLRS = .responseText
    Debug.Print "RESPONSETEXT-------------------------------------------"
    .abort
End With

Set Parsed = JsonConverter.ParseJson(apiWaxLRS)
sToken = Parsed("access_token")

sURL = "https://graph.microsoft.com/v1.0/users/" & sLine1 & "/onenote/notebooks/1-b3a45a8f-16e4-4d3d-a5c7-ef114f0ddada/sections" '
    
    With CreateObject("WinHttp.WinHttpRequest.5.1")
        .Open "GET", sURL, False
        .setRequestHeader "Host", "graph.microsoft.com"
        .setRequestHeader "Authorization", "Bearer " & sToken
        .send
        apiWaxLRS = .responseText
        sToken = ""
        sFile2 = ""
        sFile3 = ""
        sFile4 = ""
        sURL = ""
        sLine1 = ""
        sLine2 = ""
        sLine3 = ""
        sLine4 = ""
        apiWaxLRS = .responseText
        Debug.Print apiWaxLRS
        Debug.Print "--------------------------------------------"
        .abort
    End With
    
    Set Parsed = JsonConverter.ParseJson(apiWaxLRS)
    Set vDetails = Parsed("value")
    For Each rep In vDetails
        sONID = rep("id")
        sCreatedDateTime = rep("createdDateTime")
        sSelfLink = rep("self")
        sDisplayName = rep("displayName")
        sLastModifiedDateTime = rep("lastModifiedDateTime")
        sIsDefault = rep("isDefault")
        sUserRole = rep("pagesUrl")
        sPSGODContext = rep("parentSectionGroup@odata.context")
        sIsShared = rep("parentNotebook@odata.context")
        
        Set rep1 = rep("createdBy")
        Set rep2 = rep1("user")
        sCBID = rep2("id")
        sCBDisplayName = rep2("displayName")
        Set rep3 = rep("lastModifiedBy")
        Set rep4 = rep1("user")
        sCBLastModifiedID = rep4("id")
        sCBLastModifiedName = rep4("displayName")
        Set rep5 = rep("links")
        Set rep6 = rep5("oneNoteClientUrl")
        sCBLClientURL = rep6("href")
        Set rep7 = rep("links")
        Set rep8 = rep5("oneNoteWebUrl")
        sCBLWebURL = rep8("href")
        Set rep9 = rep("parentNotebook")
        sPNID = rep9("id")
        sPNDisplayName = rep9("displayName")
        sPNSelf = rep9("self")
        
        Debug.Print "sONID = " & sONID
        Debug.Print "sSelfLink = " & sSelfLink
        Debug.Print "sCreatedDateTime = " & sCreatedDateTime
        Debug.Print "sDisplayName = " & sDisplayName
        Debug.Print "sLastModifiedDateTime = " & sLastModifiedDateTime
        Debug.Print "sIsDefault = " & sIsDefault
        Debug.Print "sUserRole = " & sUserRole
        Debug.Print "sCBID = " & sCBID
        Debug.Print "sCBDisplayName = " & sCBDisplayName
        Debug.Print "sCBLastModifiedName = " & sCBLastModifiedName
        Debug.Print "sCBLastModifiedID = " & sCBLastModifiedID
        Debug.Print "sCBLClientURL = " & sCBLClientURL
        Debug.Print "sCBLWebURL = " & sCBLWebURL
        Debug.Print "sPNID = " & sPNID
        Debug.Print "sPNDisplayName = " & sPNDisplayName
        Debug.Print "sPNSelf = " & sPNSelf
        Debug.Print "sPSGODContext = " & sPSGODContext
        Debug.Print "sIsShared = " & sIsShared
        Debug.Print "--------------------------------------------"
    Next
    

End Function
