msgraph-sdk-go只响应odata上下文,而不是Data本身



我想使用Golang Graph SDK请求用户的日历,但我只是得到随机的Odata值作为响应,而不是Json或其他格式。

首先,我使用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("redacted@domain.com").CalendarView().Get(context.Background(), &options)
if err != nil {
// const colorRed = "33[0;31m"
fmt.Println(err)
}
fmt.Println(result)
}

我只是得到这样的响应:

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

我期待一个Json输出,而不是随机数据上下文值。有什么明显的东西我没注意到吗?Oxc的计数....值改变,如果我改变StartDateTime和EndDateTime。

您不应该访问result对象,因为您暴露了内部struct响应细节,而无法正确导航其属性。

您应该使用GetValue检索响应,它将返回Eventable对象的一个切片,您可以在其上迭代获得所需的日历属性:

func main() {
// code omitted as being unchanged
// ...
_ = options
result, err := client.UsersById("redacted@domain.com").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
}
}

我使用了来自@tmarwen的信息,并在我的代码的另一个变体中添加了循环:

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
}

相关内容

  • 没有找到相关文章

最新更新