When you look at Google’s JavaScript helper object in the index.html page of their Google+ API PHP client, there is a function called connectServer(). This function makes an AJAX call using $.ajax to send the one-time use code back to the signup.php file so that the server can send this one-time code to google to obtain the access-token, timeout, and refresh token.
I wanted to move this AJAX call to my Angular app. I was able to do this successfully – see the following post, Google Plus PHP Quickstart login using Angularjs. Overall, this worked, but I did have some issues with some variables not updating in the view and wasn’t able to use $location.path(‘..’); to change pages. Using the $scope.$apply() function, I was able to eventually to get this to work, but sometimes getting the Angular digest errors.
After changing my AJAX call using Angular’s $http service, I no longer had to use the $scope.$apply. Everything now worked. Here’s the code in my controller of that AJAX call using the $http service:
NOTE: see my previous post on how you can get the JavaScript running on the Google Plus page to run your Angular function. I, also, had to add to the parameters to include the anti-forgery token.
$scope.onSignInCallback = function(authResult, afToken){
console.log('controller code got called!');
console.log(authResult);
// I am storing Google Plus access token here
State.gpplus = {};
State.gpplus.accessToken = authResult['access_token'];
$http({
method: 'POST',
url: 'http://localhost/storycreate/sql/auth/signin.php' + '/connect?state=' + afToken,
headers: {'Content-Type' : 'application/octet-stream; charset=utf-8'},
data: authResult.code
})
.success(function(result) {
// signin.php confirmed the one-time code and now the server has Google's access-token
// The server stores that and the timeout in it's mysql database. I have it create a new
// access token (using md5(rand())) and store it in the same sql record I have the google
// access token and timeout. Now I pass that new access-token back to the client
// so that google's access token never crosses the wire between the client and my server.
console.log("success");
console.log(result);
var $ary = result.split(":");
State.accessToken = $ary[1];
$location.path('/list');
})
.error(function(e) {
console.log('error in onSignInCallback');
console.log(e);
});
};