Swift/Firebase 数据库无效令牌错误

Swift/Firebase Database invalid token error

遇到 Firebase 的一些相当奇怪的问题。我已经通过 cocoaPods 安装了 firebase,包括 GoogleService-Info.plist 等 - 但是当我尝试从 Firebase 读取值时,我收到此错误:

2016-06-16 12:10:04.598 MP[78421:13128189] [FirebaseDatabase] Authentication failed: invalid_token (Invalid claim 'aud' in auth token.)

每次我尝试开始一个新项目时,我都会得到这个,除了一次——但我现在已经放弃了那个项目,因为我正在开始一个新项目。这是简单的代码:

import UIKit
import Firebase
import FirebaseDatabase
class ViewController: UIViewController {
    var ref:FIRDatabaseReference!
    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference()
        ref.observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in
            var val = snapshot.value! as? String
            print(val)
        }
    }
}

我该如何解决这个问题?

此错误消息表示令牌中的项目与您的应用连接的项目不匹配。来自 post on the firebase-talk group:

There are a couple ways you may hit this:

  1. You've changed your GoogleService-Info.plist to point at a different project, but your app still has a cached token from a previous run.

  2. I believe the iOS Simulator shares the keychain between apps, so if you have multiple apps using Firebase, one app might be picking up the token cached by another app. This only happens in the simulator and we're working on changing the Firebase SDK to avoid this.

In either case, you can probably resolve the issue by doing an explicit signOut() call before initializing Firebase Database.