Search code examples
javascriptcordovagoogle-drive-apiphonegap-pluginsgoogle-signin

Using the Google API javascript in Cordova / Phonegap


I'm developing an application with Cordova and would like to save files in Googe Drive.

I've got success in login to Google, using the cordova-plugin-googleplus (https://github.com/EddyVerbruggen/cordova-plugin-googleplus). However I could not get the plugin returns to me accessToken or idToken so I could use with Google javascript API.

window.plugins.googleplus.login(
    {
        'scopes': 'https://www.googleapis.com/auth/drive.file profile',
        'offline': true, 
        'webApiKey': ‘CODE’ 
    },
    function (obj) {
        $scope.$apply(function() {
            $scope.srcImage = obj.imageUrl;
            $scope.NomeGoogle = obj.displayName;
        });
    },
    function (msg) {
        alert('Erro');
        alert('error: ' + msg);
    }
);

I tried using the code below, but returned me the following error:

"Uncaught gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy"

gapi.load('auth2', function() {

    gapi.auth2.init({

        client_id: 'REVERSED_CLIENTID',

    }).then(function(){

        auth2 = gapi.auth2.getAuthInstance();
        console.log(auth2.isSignedIn.get()); //now this always returns correctly        

    });
});

Solution

  • I managed to figure out the problem, why wasn´t getting the serverAuthCode from plugin. It is necessary to create 2 credentials on the Google Developers Console. The 1st must be Android, this will be for the plugin and the 2nd should be a Web App, this is necessary to achieve serverAuthCode.

    The code looks like this

    window.plugins.googleplus.login(
        {
            'scopes': 'https://www.googleapis.com/auth/drive.file profile',
            'offline': true, 
            'webApiKey': ‘REVERSED_CODE of Web App Credential’ 
        },
        function (obj) {
            $scope.$apply(function() {
                $scope.srcImage = obj.imageUrl;
                $scope.NomeGoogle = obj.displayName;
            });
    
    
                var data = $.param({
                    client_id: 'REVERSED_CODE of Web App Credential',
                    client_secret: 'SECRET_CODE of Web App Credential',
                    grant_type: 'authorization_code',
                    code: obj.serverAuthCode
                });
    
                var config = {
                    headers : {
                        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                    }
                }
    
                $http.post("https://www.googleapis.com/oauth2/v3/token", data, config).success(function(data, status) {
                    //data.access_token;
    
    /** from now you can do use of google API **/
    
                })
                .error(function(data, status) {
                    console.log(data);
                    console.log(status);
                });
    
        },
        function (msg) {
            alert('Erro');
            alert('error: ' + msg);
        }
    );
    

    Thank you for your reply rojobo