Home > OS >  msgraph-sdk-go just responds with odata context, not the Data itself
msgraph-sdk-go just responds with odata context, not the Data itself

Time:11-24

I want to Request a Calendar of a user using the Golang Graph SDK, but i just get random Odata Values as Response instead of Json or some other format.

First i made the Connection and Request using the official Docs and Issues on Github:

package main

import (
    "context"
    "fmt"
    azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    a "github.com/microsoft/kiota-authentication-azure-go"
    msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
    cv "github.com/microsoftgraph/msgraph-sdk-go/users/item/calendarview"
)

func main() {
    cred, err := azidentity.NewClientSecretCredential(
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        nil,
    )
    auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"https://graph.microsoft.com/.default"})

    adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
    
    client := msgraphsdk.NewGraphServiceClient(adapter)
    if err != nil {
        fmt.Println("Fehler: ", err)
    }
    requestStartDateTime := "2022-11-01T00:00:00"
    requestEndDateTime := "2022-11-17T00:00:00"
    query := cv.CalendarViewRequestBuilderGetQueryParameters{
        // Orderby: []string{"start/dateTime"},
        StartDateTime: &requestStartDateTime,
        EndDateTime: &requestEndDateTime,
        Select: []string{
            "subject",
            "start",
            "end",
        },
    }
    
    options := cv.CalendarViewRequestBuilderGetRequestConfiguration{
        QueryParameters: &query,
    }
    _ = options
    result, err := client.UsersById("[email protected]").CalendarView().Get(context.Background(), &options)
    if err != nil {
        // const colorRed = "\033[0;31m"
        fmt.Println(err)
    }
    fmt.Println(result)

}

And i just get a response like that:

&{{map[@odata.context:0xc0002310d0] <nil> 0xc00043c030} [0xc0003da310 0xc0003da120 0xc0001daa11 0xc0001dad20 0xc0003dbf01 0xc0003db110 0xc0003da401 0xc0003db681 0xc0003da901 0xc0003daa81]}

I was expecting a Json output, not Random odata Context Values. is there something obvious that i have missed? The count of the Oxc.... Values changes, if i alter the StartDateTime and EndDateTime.

CodePudding user response:

You shouldn't be accessing the result object as is as you are exposing the internal struct response details without being able to navigate its properties properly.

You should retrieve the response using GetValue which would return a slice of Eventable objects over which you can iterate getting the desired calendar properties:

func main() {
    // code omitted as being unchanged
    // ...

    _ = options
    result, err := client.UsersById("[email protected]").CalendarView().Get(context.Background(), &options)
    if err != nil {
        fmt.Println(err)
    }

    for _, event := range result.GetValue() {
        iCalId := event.GetICalUId()
        startTime := event.GetStart()
        endTime := event.GetEnd()
        fmt.Printf("iCalId: %s | Time: (%s - %s) | \n", *iCalUId, *startTime.GetDateTime(), *endTime.GetDateTime()) // or any other properties
    }
}

CodePudding user response:

I used the Information from @tmarwen and added the Loop in another variation to my code:

    for _, event := range result.GetValue() {
    fmt.Printf("Subject: %s | Time: (%s - %s) | \n", *event.GetSubject(), *event.GetStart().GetDateTime(), *event.GetEnd().GetDateTime()) // or any other properties
}
  • Related