Firebase (iOS) : 「Network error while to fetch VERIFY_CREDENTIAL from TWITTER.」というエラーが出てFIRUserが作成できない

広告:超オススメUnity Asset
  広告:超オススメUnity Asset

Twitterログインを使用してFirebaseにユーザー(FIRUser)を作成しようとした場合に、Twitterのログインは成功しているものの FIRAuth.auth()?.signIn(with: credential 時に以下のようなメッセージのエラーが発生してしまう問題にぶつかりました。

Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x608000253d40 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
    code = 400;
    errors =     (
                {
            domain = global;
            message = "Network error while to fetch VERIFY_CREDENTIAL from TWITTER.";
            reason = invalid;
        }
    );
    message = "Network error while to fetch VERIFY_CREDENTIAL from TWITTER.";
}}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}

これはこのメッセージだけではわかりません。原因はFabricのようです。以下のサイトにズバリ回答が書かれていました。

AppDelegateでAPIキーとAPIシークレットを改めて設定してあげる必要がありそうです。これにならって修正しようと思いますが、ついでにinfo.plistに以下のようにキーとシークレットの文字列を書き込む形(「TwitterKey」というDictionaryを作り、「TwitterConsumerKey」「TwitterConsumerSecret」という文字列を内包する感じにしました。)にしてそれをコードで拾うような形にして設定してみようと思います。

00

AppDelegateの FIRApp.configure() まではこんな感じです。

let twKey = Bundle.main.infoDictionary!["TwitterKey"] as! Dictionary<String,AnyObject>
let TwitterConsumerKey = twKey["TwitterConsumerKey"]! as! String
let TwitterConsumerSecret = twKey["TwitterConsumerSecret"]! as! String
Twitter.sharedInstance().start(withConsumerKey: TwitterConsumerKey, consumerSecret: TwitterConsumerSecret)

Fabric.with([Twitter.self])
FIRApp.configure()

以上の修正を加えて実行するとうまくいくようになると思います。