Created
December 2, 2015 18:04
-
-
Save goliatone/b5e3f6b00527007e14dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var googleapi = { | |
| authorize: function(options) { | |
| var deferred = $.Deferred(); | |
| //Build the OAuth consent page URL | |
| var authUrl = 'https://accounts.google.com/o/oauth2/auth?' + $.param({ | |
| client_id: options.client_id, | |
| redirect_uri: options.redirect_uri, | |
| response_type: 'code', | |
| scope: options.scope | |
| }); | |
| //Open the OAuth consent page in the InAppBrowser | |
| var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no'); | |
| //The recommendation is to use the redirect_uri "urn:ietf:wg:oauth:2.0:oob" | |
| //which sets the authorization code in the browser's title. However, we can't | |
| //access the title of the InAppBrowser. | |
| // | |
| //Instead, we pass a bogus redirect_uri of "http://localhost", which means the | |
| //authorization code will get set in the url. We can access the url in the | |
| //loadstart and loadstop events. So if we bind the loadstart event, we can | |
| //find the authorization code and close the InAppBrowser after the user | |
| //has granted us access to their data. | |
| $(authWindow).on('loadstart', function(e) { | |
| var url = e.originalEvent.url; | |
| var code = /\?code=(.+)$/.exec(url); | |
| var error = /\?error=(.+)$/.exec(url); | |
| if (code || error) { | |
| //Always close the browser when match is found | |
| authWindow.close(); | |
| } | |
| if (code) { | |
| //Exchange the authorization code for an access token | |
| $.post('https://accounts.google.com/o/oauth2/token', { | |
| code: code[1], | |
| client_id: options.client_id, | |
| client_secret: options.client_secret, | |
| redirect_uri: options.redirect_uri, | |
| grant_type: 'authorization_code' | |
| }).done(function(data) { | |
| deferred.resolve(data); | |
| }).fail(function(response) { | |
| deferred.reject(response.responseJSON); | |
| }); | |
| } else if (error) { | |
| //The user denied access to the app | |
| deferred.reject({ | |
| error: error[1] | |
| }); | |
| } | |
| }); | |
| return deferred.promise(); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="format-detection" content="telephone=no" /> | |
| <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> | |
| <preference name="DisallowOverscroll" value="true" /> | |
| <preference name="UIWebViewBounce" value="false" /> | |
| </head> | |
| <body> | |
| <div class="content" style="margin-top:23px;"> | |
| <div class="row"> | |
| <div id="login"> | |
| <a>Sign In With Google!</a> | |
| <p></p> | |
| </div> | |
| <script type="text/javascript" src="js/jquery.js"></script> | |
| <script type="text/javascript" src="js/index.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function oauthsetup(){ | |
| var $loginButton = $('#login a'); | |
| var $loginStatus = $('#login p'); | |
| $loginButton.on('click', function() { | |
| googleapi.authorize({ | |
| client_id: '<CLIENT_ID>', | |
| client_secret: '<CLIENT_SECRET>', | |
| redirect_uri: 'http://localhost', | |
| scope: 'profile email' | |
| }).done(function(data) { | |
| $loginStatus.html('Access Token: ' + data.access_token); | |
| }).fail(function(data) { | |
| $loginStatus.html(data.error); | |
| }); | |
| }); | |
| } | |
| document.addEventListener("deviceready", oauthsetup, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment