【问题标题】:Setting parameters for urlRequest with TwitterKit for Direct Message in Swift在 Swift 中使用 TwitterKit 为直接消息设置 urlRequest 的参数
【发布时间】:2018-09-14 13:10:58
【问题描述】:

使用 Swift,我可以使用以下代码毫无问题地发布推文:

func postTweet(tweet : String) {
    if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
        var clientError : NSError?
        let session = TWTRTwitter.sharedInstance().sessionStore.session()

        if let userid = session?.userID {

            let client = TWTRAPIClient(userID: userid)
            let urlEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
            let params = ["status": "This works fine."]

            let request = client.urlRequest(withMethod: "POST",
                 urlString: urlEndpoint, 
                 parameters: params, error: &clientError)

            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
                if connectionError != nil {
                    print("Error: \(String(describing: connectionError?.localizedDescription))")
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: [])
                    print("json: \(json)")

                } catch let jsonError as NSError {
                    print("json error: \(jsonError.localizedDescription)")
                }
            }
        }
    }
}

-

但是,当我更改 urlEndpoint 和参数以发送直接消息时,我最终会在应用程序中崩溃。下面的错误消息&我不知道为什么。任何帮助表示赞赏。

func postDM(tweet : String) {
    if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
        var clientError : NSError?
        let session = TWTRTwitter.sharedInstance().sessionStore.session()
        let statusesShowEndpoint = "https://api.twitter.com/1.1/direct_messages/events/new.json"

        // '{"event": {"type": "message_create", "message_create": {"target": {"recipient_id": "RECIPIENT_USER_ID"}, "message_data": {"text": "Hello World!"}}}}'

        let param = [
            "event": [
                "type":"message_create",
                "message_create": [
                    "target": ["recipient_id": "6WCbot"],
                    "message_data": ["text": "\(tweet)"]
                ]
            ]
        ]

        if let userid = session?.userID {

            let client = TWTRAPIClient(userID: userid)
            //let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
            //let param = ["status": "Terceiro teste?"]
            //print (param)

            // CRASH HERE
            var request = client.urlRequest(withMethod: "POST", urlString: statusesShowEndpoint, parameters: param, error: &clientError) 

            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = try? JSONSerialization.data(withJSONObject: param, options: [])

            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
                if connectionError != nil {
                    print("Error: \(String(describing: connectionError?.localizedDescription))")
                }

                do {
                    if data != nil {
                        let json = try JSONSerialization.jsonObject(with: data!, options: [])
                        print("json: \(json)")
                    }

                } catch let jsonError as NSError {
                    print("json error: \(jsonError.localizedDescription)")
                }
            }
        }
    }
}

我得到的“无法识别的选择器”错误消息是


2018-09-14 14:08:55.697266+0200 CompanionApp[35050:15292775] -[_TtGCs26_SwiftDeferredNSDictionarySSP__ stringByAddingPercentEncodingWithAllowedCharacters:]:无法识别 选择器发送到实例 0x60800023d200 2018-09-14 14:08:55.706728+0200 6WCCompanion[35050:15292775] *** 终止应用程序 由于未捕获的异常“NSInvalidArgumentException”,原因: '-[_TtGCs26_SwiftDeferredNSDictionarySSP__ stringByAddingPercentEncodingWithAllowedCharacters:]:无法识别 选择器发送到实例 0x60800023d200'

【问题讨论】:

    标签: swift twitter twitterkit


    【解决方案1】:

    请使用此格式

    { “事件”: { "type": "message_create", “消息创建”:{ “目标”: { “recipient_id”:“844385345234” }, “消息数据”:{ "text": "嗨,我叫乔恩。有什么可以帮忙的吗?", }, “custom_profile_id”:“100001” } } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-28
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多