LoginSignup
2
1

More than 5 years have passed since last update.

Swifterを使ってTwitterにDictionary<String, String>をそのまま投稿しようとすると401が返ってくる

Last updated at Posted at 2016-04-30

始めに

Swifter を使ってSwiftの Dictionary<String, String> をそのまま投稿しようとしたら弾かれた。

Swifterとは

Swifter とは、Pure SwiftなTwitterクライアント作成用のライブラリ。
割とちゃんとメンテされてる感じなのにCocoaPodsにもCarthageにも対応してないのが不思議。
(ちなみに、CocoaPodsに関しては対応するPRがコンフリクトしたまま放置されている)

なお、同じ名前のサーバーエンジン があり、
しかもこちらはCocoaPodsに対応している。
Swifterをpod installすると、サーバーエンジンがインストールされてしまうので要注意。

最初はFabricのTwitterKitを使おうとしたのだが、
ユーザー操作無しで固定IDで認証 & ツイートする方法がどうにもわからなかったのでこちらを使うことにした。

準備

Swifter のインポート及び、ConsumerKey, AccessTokenの取得は済ませておく。
ここでは手順は書かない。

失敗するコード

Test.swift
class Test: NSObject {
    func test() {
        let swifter = Swifter(consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET, oauthToken: TWITTER_ACCESS_TOKEN, oauthTokenSecret: TWITTER_ACCESS_TOKEN_SECRET)
        let dictionary = ["hoge" : "fuga"]
        let string = dictionary.description
        swifter.postStatusUpdate(string, success: { status in
        }, failure: { error in
            print(error)
        })
    }
}

結果

/* 投稿した文字列 */
["hoge": "fuga"]

/* 返って来たエラー */
Error Domain=NSURLErrorDomain Code=401 "HTTP Status 401: Unauthorized, Response: {"errors":[{"code":32,"message":"Could not authenticate you."}]}" UserInfo={Response-ErrorCode=32, NSLocalizedDescription=HTTP Status 401: Unauthorized, Response: {"errors":[{"code":32,"message":"Could not authenticate you."}]}, Response-Headers=<CFBasicHash 0x7f8003d01ee0 [0x1135a2a40]>{type = immutable dict, count = 10,
entries =>
    0 : Server = tsa_a
    1 : Content-Type = <CFString 0x7f8001eadaf0 [0x1135a2a40]>{contents = "application/json; charset=utf-8"}
    2 : Set-Cookie = <CFString 0x7f8001eac7d0 [0x1135a2a40]>{contents = "guest_id=v1%3A146204234384616421; Domain=.twitter.com; Path=/; Expires=Mon, 30-Apr-2018 18:52:23 UTC"}
    3 : Strict-Transport-Security = <CFString 0x7f8003e48890 [0x1135a2a40]>{contents = "max-age=631138519"}
    4 : x-tsa-request-body-time = 0
    5 : x-connection-hash = <CFString 0x7f8003e55cf0 [0x1135a2a40]>{contents = "c39f9646d5657d9e8c0921a2a2c0abf7"}
    6 : Date = <CFString 0x7f8001ee9350 [0x1135a2a40]>{contents = "Sat, 30 Apr 2016 18:52:23 GMT"}
    7 : x-response-time = 9
    8 : Content-Encoding = gzip
    10 : Content-Length = 89
}

考察

同じ文字列をブラウザやTweetbotから投稿しようとしたら普通に投稿できた。
また、"hoge : fuga"は問題なく投稿できることが確認できた。
このライブラリが内部でおかしなことをしているのか、投稿できたクライアントが特殊な処理をしているのか、
もしくはこのライブラリが必要な処理をしていないのか、は未調査。
わかったら追記する。(御存じの方がいらっしゃったら、御教示ください。)

終わりに

この辺のことを修正してPR出せば、OSS contribute処女を喪失できるかも、とか思った。

(2016/05/19 追記)

[] が含まれているとエラーが出るようだったので、こんなPRを作ったが現在放置中。
https://github.com/mattdonnelly/Swifter/pull/141

(2016/08/12 追記)

マージされて直った :tada:
https://github.com/mattdonnelly/Swifter/pull/141

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1