由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入中的类型无效(MSGraphDateTimeTimeZone)”

问题描述 投票:0回答:1

我正在发送创建 MS Graph 事件 API 的请求。下面是我的功能。当我执行它时,我收到“

Invalid type in JSON write (MSGraphDateTimeTimeZone)
”错误或“
Invalid type in JSON write (MSGraphAttendee)
”。

我正在使用他们的 Objective-C 请求示例,所以不确定我做错了什么 - https://learn.microsoft.com/en-us/graph/api/group-post-events?view=graph -rest-1.0&tabs=objc

public func reserveHotel(email: String, startDateTime: Date, endDateTime: Date, locationName: String, bodyText: String? = nil) -> Promise<Data> {
    let request = makeRequest(relativeUrl: "/me/calendar/events")
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("outlook.timezone=\"UTC\"", forHTTPHeaderField: "Prefer")
    
    let event = MSGraphEvent()
    event.subject = "Test"
    event.isOrganizer = true
    
    event.start = MSGraphDateTimeTimeZone.fromDate(startDateTime)
    event.end = MSGraphDateTimeTimeZone.fromDate(endDateTime)
    
    if let eventText = bodyText {
        let body = MSGraphItemBody()
        body.contentType = MSGraphBodyType.html()
        body.content = eventText
        event.body = body
    }
    
    let location = MSGraphLocation()
    location.displayName = locationName
    event.location = location
    
    let attendee = MSGraphAttendee()
    let emailMS = MSGraphEmailAddress()
    emailMS.address = email
    emailMS.name = locationName
    attendee.emailAddress = emailMS
    attendee.type = MSGraphAttendeeType.required()
    event.attendees = [attendee]
    
    do {
        request.httpBody = try event.getSerializedData()
    } catch {
        log.error("Failed to create JSON request body for Microsoft Graph: \(error)")
        return Promise(error: error)
    }
    
    return processRequest(request)
}

private func processRequest(_ request: NSMutableURLRequest) -> Promise<Data> {
    return Promise { seal in
        let dataTask = MSURLSessionDataTask(request: request, client: httpClient, completion: { (data: Data?, response: URLResponse?, error: Error?) in
            // Check for server-side errors
            if let response = response as? HTTPURLResponse {
                let statusCode = response.statusCode
                if (statusCode < 200) || (statusCode > 299) {
                    let description = "Microsoft Graph service returned HTTP response status \(statusCode) for URL \(request.url?.absoluteString ?? "")"
                    seal.reject(NSError(domain: NSURLErrorDomain, code: statusCode, userInfo: [NSLocalizedDescriptionKey: description]))
                    return
                }
            }
            
            seal.resolve(data, error)
        })
        dataTask?.execute()
    }
}
json swift microsoft-graph-api
1个回答
0
投票

这是 SDK 中的一个错误。这将解决这个问题: https://github.com/microsoftgraph/msgraph-sdk-objc-models/pull/36

© www.soinside.com 2019 - 2024. All rights reserved.