如何在 OS X Cocoa 应用程序中向 Google 授权用户

2024-03-08

我在用Firebase https://www.firebase.com在我的 OS X 应用程序中。我正在尝试添加 Google 身份验证。这是一iOS 示例 https://www.firebase.com/docs/ios/guide/login/google.html.

问题是:如何在 OS X 应用程序中获取 Google OAuth 访问令牌?


Firebase 身份验证可以通过以下方式与 OS X 上的 Google 登录集成:GTMOAuth2 pod.

import Cocoa
import GTMOAuth2
import Firebase

class MainWindowController: NSWindowController {

let ref = Firebase(url: "https://xyz.firebaseio.com")

override func windowDidLoad() {
    super.windowDidLoad()


    let frameworkBundle = NSBundle(forClass: GTMOAuth2WindowController.self)
    let windowController = GTMOAuth2WindowController(scope: "", clientID: clientID, clientSecret: clientSecret, keychainItemName: keychainName, resourceBundle: frameworkBundle)
    windowController.signInSheetModalForWindow(window, delegate: self, finishedSelector: #selector(MainWindowController.didFinishWithAuth(windowController:auth:error:)))

}

func didFinishWithAuth(windowController wc:GTMOAuth2WindowController, auth: GTMOAuth2Authentication, error: NSError?) {
    if error != nil {
        print(error)
    } else {
        print(auth)
        ref.authWithOAuthProvider("google", token: auth.accessToken, withCompletionBlock: { err, auth in
            if err != nil {
                print(err)
            } else {
                print(auth)
            }
        })
      }
  }
}

有几件事需要注意,正如上面无脑提到的,您必须使用以下命令创建 OAuth Api 密钥other凭据管理器中的选项。您必须记住在 Firebase 项目中将您的 clientID 列入白名单。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 OS X Cocoa 应用程序中向 Google 授权用户 的相关文章

随机推荐