2014-06-10 10 views
7

Sto tentando di accedere dalla mia app Phonegap utilizzando Angularjs (utilizzando lo Ionic Framework) tramite Google OAuth2. Attualmente sto usando il http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html per l'accesso. Ma sta creando codice davvero brutto e piuttosto difficile da capire quando sto usando Angular-UI-Router per Ionic.Come accedere con Google OAuth2 utilizzando Angularjs all'interno di Phoneegap utilizzando ClientID e ClientSecret

Questo numero sembra aggirarsi senza risposte adeguate. Spero che dovrebbe essere risolto ora. I Google Angular Guys dovrebbero aiutare. How to implement Google Auth in phonegap?

L'argomento più vicino è How to use Google Login API with Cordova/Phonegap, ma questa non è una soluzione per angularjs.

ho dovuto trasferire i valori delle variabili JavaScript utilizzando il seguente codice:

 var el = document.getElementById('test'); 
     var scopeTest = angular.element(el).scope(); 
     scopeTest.$apply(function(){ 
      scopeTest.user = user; 
      scopeTest.logged_in = true; 
      scopeTest.name = user.name; 
      scopeTest.email = user.email; 
     }); 

risposta

18

Ho fatto la soluzione di questo tipo, in cui TestCtrl è il controller in cui risiede il pulsante di accesso. C'è un mix di chiamate $ .ajax basate su jquery, che cambierò in modo anguale. La funzione google_call chiama fondamentalmente google_api, che è menzionato nel link citato sopra in phonegap-tips.

.controller('TestCtrl', function($scope,$ionicPopup) { 
$scope.logged_in = false; 
$scope.getMember = function(id) { 
    console.log(id); 
}; 
$scope.test = function(){ 
    $ionicPopup.alert({"title":"Clicked"}); 
} 

$scope.call_google = function(){ 
    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); 
     //$ionicPopup.alert({"title":JSON.stringify(data)}); 
     $scope.getDataProfile(); 
    }); 
}; 
$scope.getDataProfile = function(){ 
    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. 
      window.localStorage.gmailLogin="true"; 
      window.localStorage.gmailID=data.id; 
      window.localStorage.gmailEmail=data.email; 
      window.localStorage.gmailFirstName=data.given_name; 
      window.localStorage.gmailLastName=data.family_name; 
      window.localStorage.gmailProfilePicture=data.picture; 
      window.localStorage.gmailGender=data.gender; 
      window.localStorage.gmailName=data.name; 
      $scope.email = data.email; 
      $scope.name = data.name; 
      } 
     }); 
     //$scope.disconnectUser(); //This call can be done later. 
}; 
$scope.disconnectUser = function() { 
    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 
    } 
    }); 
}; 
}) 

sto fornendo questa risposta per i neofiti che hanno affrontato problemi simili come la mia, cercando di effettuare il login utilizzando Google OAuth2. Quindi chiedendo Upvotes spudoratamente come sono nuovo anche qui!

+0

Come un collega che chiedeva noob, c'era un motivo per cui hai usato la chiamata jQuery ajax su $ http di Angular, get (...)? –

+0

In realtà questo può essere molto sostituito con $ http.get (....) con una funzione di successo e una funzione di errore. Anche io non lo sapevo quando l'ho fatto! Grazie per la menzione. – tor9ado

Problemi correlati