如何在OS X Cocoa应用程序中使用Google授权用户 [英] How to authorize user with Google in OS X Cocoa application
问题描述
我在OS X应用程序中使用 Firebase .我正在尝试添加Google身份验证.这是适用于iOS的示例.
I am using Firebase in my OS X application. I am trying to add Google Authentication. This is an example for iOS.
问题是:如何在OS X应用程序中获取Google OAuth访问令牌?
Question is: How to obtain Google OAuth access token in OS X application?
推荐答案
可以在Objective-C中获取Google OAuth 2令牌. GTMOAuth2 .使用cocoapods:
It is possible to obtain Google OAuth 2 token in Objective-C with. GTMOAuth2. Using cocoapods:
pod 'GTMOAuth2'
GTMOAuth2库需要应用程序类型为other
的客户端ID.可以在Google Developer Console中创建一个:
GTMOAuth2 library needs client id with application type other
. It is possible to create one in Google Developer Console:
这是一个代码示例,描述了如何使用此lib:
This a code sample describing how to work with this lib:
#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.");
}
}];
}
它将在第一次启动时创建带有Google授权页面的弹出窗口,并使用存储在钥匙串中的令牌" 进行后续运行.
It will create pop-up window with Google authorization page inside on a first launch and use "token" stored in keychain for subsequent runs.
该授权人几乎可以用于所有Google服务.
This authorizer can be used with almost every Google Service.
似乎无法轻松地将其与Firebase集成.
Looks like it can not be easily integrated with Firebase.
这篇关于如何在OS X Cocoa应用程序中使用Google授权用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!