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

kis*_*eno 5 macos cocoa objective-c google-authentication firebase

我在我的 OS X 应用程序中使用Firebase。我正在尝试添加 Google 身份验证。这是iOS示例

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

kis*_*eno 2

可以在 Objective-C 中获取 Google OAuth 2 令牌。GTMOAuth2。使用cocoapods:

pod 'GTMOAuth2'
Run Code Online (Sandbox Code Playgroud)


GTMOAuth2 库需要具有应用程序类型的客户端 ID other。可以在 Google 开发者控制台中创建一个:

在此输入图像描述


这是描述如何使用此库的代码示例:

#import "GTMOAuth2Authentication.h"
#import "GTMOAuth2WindowController.h"

...

- (void) applicationDidFinishLaunching:(NSNotification *) aNotification {
    GTMOAuth2Authentication * = [GTMOAuth2WindowController
               authForGoogleFromKeychainForName: @"where-to-store-token-in-a-keychain" 
                                       clientID: @"client-id"
                                   clientSecret: @"client-secret"];

    if (authorizer.canAuthorize) {
        NSLog(@"Your authorizer was restored from key chain and can be autorized. Authorozer: %@", authorizer);
    }
    else {
        NSBundle * frameworkBundle = [NSBundle bundleForClass:[GTMOAuth2WindowController class]];
        GTMOAuth2WindowController * windowController;
        windowController = [GTMOAuth2WindowController controllerWithScope: @"" //scope url here, empty is just email and profile
                                                             clientID: clientID
                                                         clientSecret: clientSecret
                                                     keychainItemName: kKeychainItemName
                                                       resourceBundle: frameworkBundle];

        [windowController signInSheetModalForWindow: [self window]
                              completionHandler: ^(GTMOAuth2Authentication * auth, NSError * error) {
                                                    if (error == nil) {
                                                        authorizer = auth;
                                                        NSLog(@"Successfully signed in.");
                                                    } else {
                                                        NSLog(@"Failed to sign in.");
                                                    }
                              }];
}
Run Code Online (Sandbox Code Playgroud)

它将在首次启动时创建带有 Google 授权页面的弹出窗口,并使用存储在钥匙串中的“令牌”进行后续运行。


优点

该授权者几乎可用于所有 Google 服务。

缺点

看起来它不能轻松地与 Firebase 集成。