How to use Google Login API with Cordova/Phonegap
我想在我的Phonegap应用中使用"使用Google登录"。我读了很多文章,但找不到如何完成。提前致谢。我尝试根据此URL将oAuth2用于"已安装的应用程序"。但是随后,应用程序用户必须手动复制代码并将其粘贴到我的应用程序中。
我正在使用built.io联合登录(如果相关)。
Google放弃了对上面接受的答案的支持! 2017年4月20日之后,将不再支持使用@Deep Mehta所述的应用内浏览器。如果您使用接受的答案,那么它将很快开始失败。
有关此更改的Google帖子如下:
https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html
幸运的是,有一个新的插件可以package您执行此操作所需的所有功能:
https://github.com/EddyVerbruggen/cordova-plugin-googleplus
在NPM上
https://www.npmjs.com/package/cordova-plugin-googleplus
这是一篇有关如何在Ionic 1和Ionic 2中使用它的文章:
http://blog.ionic.io/google-oauth-changes
再次-请勿使用接受的答案!它会在2017年4月20日之后失败。
将此代码添加到一个js文件中,并包含在您的项目中。当您要访问按钮上的google login api时,单击调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 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); $("#loginStatus").html('Name: ' + data.given_name); }).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(); } }; var accessToken; var UserData = null; function callGoogle() { // alert('starting'); googleapi.authorize({ client_id: 'client_id', client_secret: 'Client_Secret', redirect_uri: 'http://localhost', scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email' }).done(function(data) { accessToken = data.access_token; // alert(accessToken); // $loginStatus.html('Access Token: ' + data.access_token); console.log(data.access_token); console.log(JSON.stringify(data)); getDataProfile(); }); } // This function gets data of user. function getDataProfile() { var term = null; // alert("getting user data="+accessToken); $.ajax({ url: 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + accessToken, type: 'GET', data: term, dataType: 'json', error: function(jqXHR, text_status, strError) {}, success: function(data) { var item; console.log(JSON.stringify(data)); // Save the userprofile data in your localStorage. localStorage.gmailLogin ="true"; localStorage.gmailID = data.id; localStorage.gmailEmail = data.email; localStorage.gmailFirstName = data.given_name; localStorage.gmailLastName = data.family_name; localStorage.gmailProfilePicture = data.picture; localStorage.gmailGender = data.gender; } }); disconnectUser(); } function disconnectUser() { var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken; // Perform an asynchronous GET request. $.ajax({ type: 'GET', url: revokeUrl, async: false, contentType:"application/json", dataType: 'jsonp', success: function(nullResponse) { // Do something now that user is disconnected // The response is always undefined. accessToken = null; console.log(JSON.stringify(nullResponse)); console.log("-----signed out..!!----" + accessToken); }, error: function(e) { // Handle the error // console.log(e); // You could point users to manually disconnect if unsuccessful // https://plus.google.com/apps } }); } |
我推荐这个Cordova插件:https://www.npmjs.com/package/cordova-plugin-googleplus这是最近的事情,但我刚刚将其添加到我的应用程序中,似乎可以解决问题!
现在是2019年:并且Google API已关闭。
Google上有一篇有关如何使用Firebase API进行身份验证的文章。
在此处运行良好的另一个实现:https://github.com/valenzia10/PhonegapGoogleLogin