首页 文章

如何在Cordova中使用org.chromium.identity进行原生谷歌连接

提问于
浏览
0

我想使用原生谷歌加号功能来连接我的phonegap / cordova应用程序(android和IOS) . 通常在Phonegap中,我们使用InAppBrowser来包含Web弹出窗口 . 它已经完成,但假设用户使用Web UI通过电子邮件和密码登录 . 所以在Android中,我们使用谷歌播放和IOS相同 .

在1月,现在可以在Cordova中使用Chrome应用程序 . http://blog.chromium.org/2014/01/run-chrome-apps-on-mobile-using-apache.html

许多cordova插件都可用,特别是chrome.identity:https://github.com/MobileChromeApps/mobile-chrome-apps/tree/master/chrome-cordova/plugins/chrome.identity

如何在ChromeApps之外使用此插件进行原生谷歌登录?我安装了插件但没有样本来访问chrome对象和Api .

谢谢你的帮助

1 回答

  • 0

    这是一个古老的问题,但我认为我应该发布我的解决方案,因为我今晚点击了...下面是我如何使用插件进行身份验证并调用服务API . 抱歉,如果不是那么高效,那就是漫漫长夜:P(这是现在在Cordova启动器模板上构建的)

    var app = { 
    onDeviceReady: function() {
        /*********************
        *   I attached an EventListener to a link so I kept this binding
        *   Helps when testing to have a link handy to revoke the token
        **********************/
        app.receivedEvent('deviceready');
    
        /****************************************************************************
        *   The documentation is a little odd here, but since this isn't
        *   a Chrome app, we need to set the Manifest JSON manually.
        *
        *   The scopes are to only modify and read a calendar, not sure I need both
        *   but what the hell...
        ****************************************************************************/
        chrome.runtime.setManifest({
          oauth2: {
             // ClientID obtained from the Developers Console
            client_id: clientID,
            scopes: [ 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly' ]
          }
        },function(){
            // callback for setMainfest, empty..
            });
        /***********************************************************************
        *   Because I don't have Google Play Services setup yet I have to use
        *   the 'interactive' mode which will prompt the user to select the account,
        *   then show a permission screen. 
        *
        *   I have multiple (Google) accounts on my phone so I set the 'accountHint'
        *   to try to force authenticating my primary account and avoid having to deal
        *   with a popup.
        ******************************************************************************/
        // Just checking if I already have grabbed it, if so, no need to do it again
        if(!bToken){
            chrome.identity.getAuthToken(
                { 
                    'interactive': true,
                    'accountHint':  ACCOUNT_EMAIL,
                }, 
                function(token, acct) {
                    // set a global variable or debug...
                });
            }
    },
    }
    var calendar = {
    list: function(token) {
        // Simple AJAX call, passing the token through and dealing with the results...
        // This function just pulls calendar info...
           $.ajax({
            type: "GET",
            url: listURL,
            data: encodeURI("access_token="+token),
            cache: false,
            success:function(result) {
                // do something....
            }
        })
        }
    }
    

    希望这可以帮助!

相关问题