How to login with Google OAuth2 using Angularjs inside Phonegap using ClientID and ClientSecret

All we need is an easy explanation of the problem, so here it is.

I am trying to login from my Phonegap App using Angularjs (using the Ionic Framework) through Google OAuth2. Currently I am using the for logging in. But it is creating really ugly looking code and quite a hard to understand code when I am using Angular-UI-Router for Ionic.

This issue seems to be spiralling around without any proper answers. I hope it should be solved now. The Google Angular Guys should help.
How to implement Google Auth in phonegap?

The closest topic is How to use Google Login API with Cordova/Phonegap, but this is not a solution for angularjs.

I had to transfer the javascript variable values using the following code:

        var el = document.getElementById('test');
        var scopeTest = angular.element(el).scope();
            scopeTest.user = user;
            scopeTest.logged_in = true;

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I did the solution like this, where TestCtrl is the Controller where the Login Button resides. There is a mix of jquery based $.ajax calls, which I am going to change to the angualar way. The google_call function basically calls the google_api which is mentioned in the link mentioned above in phonegap-tips.

.controller('TestCtrl', function($scope,$ionicPopup) {
$scope.logged_in = false;
$scope.getMember = function(id) {
$scope.test = function(){

$scope.call_google = function(){
    client_id: 'CLIENT_ID',
    client_secret: 'CLIENT_SECRET',

    redirect_uri: 'http://localhost',
    scope: ''
    }).done(function(data) {
        // alert(accessToken);
        // $loginStatus.html('Access Token: ' + data.access_token);
$scope.getDataProfile = function(){
    var term=null;
    //  alert("getting user data="+accessToken);
           var item;

           // Save the userprofile data in your localStorage.
           $ =;
           $ =;
        //$scope.disconnectUser(); //This call can be done later.
$scope.disconnectUser = function() {
  var revokeUrl = ''+accessToken;

  // Perform an asynchronous GET request.
    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.
      console.log("-----signed out..!!----"+accessToken);
    error: function(e) {
      // Handle the error
      // console.log(e);
      // You could point users to manually disconnect if unsuccessful

I am providing this answer for the newbies who faced similar problems like mine while trying to login using Google OAuth2. So asking for Upvotes shamelessly as I am new here too!

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply