How to use Google Login API with Cordova/Phonegap
Google has dropped support for the accepted answer above! After April 20th 2017 use of the In-App browser as described by @Deep Mehta will no longer be supported. If you use the accepted answer then it is going to start failing very soon.
Here's Google's post about the change:
https://developers.googleblog.com/2016/08/modernizing-oauth-interactions-in-native-apps.html
Luckily there's a new plugin that wraps up all the funcitonality that you need to do this:
https://github.com/EddyVerbruggen/cordova-plugin-googleplus
and on NPM
https://www.npmjs.com/package/cordova-plugin-googleplus
Here's an article on how to use it in Ionic 1 and 2 also:
http://blog.ionic.io/google-oauth-changes
Again - DO NOT USE THE ACCEPTED ANSWER! It will fail after April 20 2017.
Using the Google API javascript in Cordova / Phonegap
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
How to redirect to Google Auth in PhoneGap Cordova?
Try to use the InAppBrowser plugin to open the external website.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/
Phonegap app runs with file://
protocol, and HTTP requests always go through the proxy server, maybe the proxy server can't process the auth request successfully.
Add Social Login (FB/Google) to Cordova Typescript project
There is some plugins for this in cordova with types folder.
This is for Facebook and this is for google.
Enjoy.
Related Topics
You Cannot Start a Load for a Destroyed Activity in Relativelayout Image Using Glide
Running a Specific Instrumentation Unit Test with Gradle
Unregistered_On_Api_Console While Getting Oauth2 Token on Android
Android Save Images to SQLite or Sdcard or Memory
What's the "Dot" for When Registering an Activity
Converting Imageproxy to Bitmap
Android:Split the Screen in 2 Equals Parts with 2 Listviews
Mkdir() Works While Inside Internal Flash Storage, But Not Sd Card
Travis Ci Failed Because Cannot Accept License Constrain Layout
Create SQLite Database in Android
How to Upload an Image in Parse Server Using Parse API in Android
Android Expandablelistview and SQLite Database
Position of Dialogfragment in Android