I am using Firebase in my OS X application. I am trying to add Google Authentication. This is an example for iOS.
Question is: How to obtain Google OAuth access token in OS X application?
Step 1: Go to Google Developers Console. If you have a Google account, login, else Create an Account. Step 2: On the dashboard, click on the Project dropdown menu. Step 3: Select an existing project or create a New Project.
Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section. Click the + button, and add a URL scheme for your reversed client ID. To find this value, open the GoogleService-Info. plist configuration file, and look for the REVERSED_CLIENT_ID key.
Firebase auth can be integrated with Google sign in on OS X the following way using the 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)
}
})
}
}
}
A couple of things to notice, as brainless mentioned above, you have to create an OAuth Api key using the other
option in the credentials manager. You have to remember to white list your clientID in your firebase project.
It is possible to obtain Google OAuth 2 token in Objective-C with. GTMOAuth2. Using cocoapods:
pod 'GTMOAuth2'
GTMOAuth2 library needs client id with application type other
. It is possible to create one in Google Developer Console:
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.");
}
}];
}
It will create pop-up window with Google authorization page inside on a first launch and use "token" stored in keychain for subsequent runs.
This authorizer can be used with almost every Google Service.
Looks like it can not be easily integrated with Firebase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With