EddyVerbruggen / cordova-plugin-googleplus

:heavy_plus_sign: Cordova plugin to login with Google Sign-In on iOS and Android
567 stars 629 forks source link

[ANDROID] - Options are not passed to android code #199

Open pranay-mathur opened 8 years ago

pranay-mathur commented 8 years ago

Options from the plugin are not passed to the GooglePlus.java file.

{ 'offline': true, 'webClientId': '<clientID>' }

These are not passed to the native code and the plugin returns this error at the time of login: -

Uncaught exception from plugin java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.optString(java.lang.String, java.lang.String)' on a null object reference at nl.xservices.plugins.GooglePlus.buildGoogleApiClient(GooglePlus.java:129) at nl.xservices.plugins.GooglePlus.execute(GooglePlus.java:68) at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:117) at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98) at org.apache.cordova.PluginManager.exec(PluginManager.java:133) at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59) at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41) at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:37) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.os.HandlerThread.run(HandlerThread.java:61)

sammugg commented 8 years ago

Hey @pranay-mathur, thanks for pointing this out.

I'm troubleshooting this issue right now. Can you answer a few questions?

  1. What version of Android are you using?
  2. How are you calling the login function? (please, provide a code snippet if possible).
  3. Did you call cordova prepare before building?
  4. How did you install the plugin? (i.e. did you use cordova-plugin-googleplus or the https:// method)

Thanks! ~Sam

pranay-mathur commented 8 years ago

@sammuggPS

  1. Android Version: - 5.1.1
  2. window.plugins.googleplus.isAvailable(function(available) { if (available) { window.plugins.googleplus.login({ 'offline': true, 'webClientId': '<webClientId>' }, function(obj) { console.log(JSON.stringify(obj)); }, function(msg) { alert('error: ' + msg); }); } });
  3. Yes, I called cordova prepare before cordova build android.
sammugg commented 8 years ago

(I added one more question before you responded, sorry. Refresh your page and check my original response)

  1. Okay, version should be fine. Are you running on an emulator? If so, make you're running the image version that includes the Google APIs. That is very important.
  2. You don't need to call isAvailable anymore. That's a legacy function that will be removed in the near future.
  3. Good!
pranay-mathur commented 8 years ago

@sammuggPS

I'm using a device, Sony Xperia Z2. I installed the plugin via https:// method.

sammugg commented 8 years ago

I just recreated the issue! I apologize.

I'll get a hotfix deployed today. Thank you so much for bringing this to my attention! I'll respond again when I've pushed out the fix.

pranay-mathur commented 8 years ago

Thanks a lot @sammuggPS

sammugg commented 8 years ago

Hey @pranay-mathur,

I just pushed the hotfix! Should be all set now. Why don't you give it another go and let me know where we stand. If this issue is resolved, please close it. If another issue arises, open it up and we'll work it out.

Thanks again for being awesome and bringing this to my attention!

Happy Coding, ~Sam

pranay-mathur commented 8 years ago

@sammuggPS

Thanks for the update, but it's still not working. The issue is with line no.129

String scopes = clientOptions.optString(ARGUMENT_SCOPES, null);

ARGUMENT_SCOPES value is null. Please check the attached screenshot.

image

pranay-mathur commented 8 years ago

@sammuggPS

Please fix this issue. I'm stuck with my android release.

EddyVerbruggen commented 8 years ago

@pranay-mathur please don't use words like 'ASAP' in an OSS repo. Thank you.

pranay-mathur commented 8 years ago

@EddyVerbruggen My humble apologies.. I'll be careful next time. Thank you for all work and support.

sammugg commented 8 years ago

@pranay-mathur,

I'm pushed an update with some cleaned up code today, but I did not resolve the warning that you have shown here. I intentionally made the fallback value null.

If you look at the if statement directly below, you'll notice that I am handling the cases of null or emptyString. A NullPointerException will not be thrown in this case. The same can be said for the webClientId, which is retrieved at line 123. Having the fallback value be null allows us to more easily catch the case of no scopes passed in.

You may also notice that a NullPointerException warning occurs in the handleSignInResult method. This can also be confidently ignored because the JSONObject.put method allows null to be passed in as the Object value. You can read more about that method here.

Happy Coding! ~Sam

sammugg commented 8 years ago

Hey @pranay-mathur, If you don't have any other questions or comments, can we go ahead and close this issue?

Best, ~Sam

pranay-mathur commented 8 years ago

@sammuggPS

Thanks again for the update. I installed the plugin using the https:// method, executed command cordova prepare and then cordova build android. While using the plugin, it's still giving the following error: -

E/PluginManager﹕ Uncaught exception from plugin java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.optString(java.lang.String, java.lang.String)' on a null object reference at nl.xservices.plugins.GooglePlus.buildGoogleApiClient(GooglePlus.java:118) at nl.xservices.plugins.GooglePlus.execute(GooglePlus.java:54) at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:117) at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98) at org.apache.cordova.PluginManager.exec(PluginManager.java:133) at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59) at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41) at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:37) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.os.HandlerThread.run(HandlerThread.java:61)

Can you please tell me if I'm missing a step while installing the plugin.

sammugg commented 8 years ago

@pranay-mathur,

What version of Java are you using? I've built using jdk 1.8 and jdk 1.7 and have not seen this issue.

Again, please make sure that your Android SDK Build-Tools, Android Platform Tools, and Android Support library are up to date.

pranay-mathur commented 8 years ago

@sammuggPS

I'm using JDK 1.8 and everything you mentioned are up to date.

pranay-mathur commented 8 years ago

@sammuggPS

The ARGUMENT_SCOPES and ARGUMENT_WEB_CLIENT_ID both are null from the start. That's why its throwing exception. But I don't understand why the parameters that I've provided are not available in the java code.

The function public boolean execute has value of only one parameter i.e., action. The value of args is null.

sammugg commented 8 years ago

@pranay-mathur,

Tell me more about your cordova set up:

Tell me more about your usage of the plugin:

Can you send me the output of your entire android build?

pranay-mathur commented 8 years ago

@sammuggPS

  1. I'm using Cordova 6.1.0.
  2. Yes, I have tried reinstalling my platform.
  3. I'm running android platform 5.1.1
  4. I have tried removing android platform and reinstalling it.

I'm not passing empty object as my first parameter in the plugin. I'm passing these as parameters

{ 'offline': true, 'webClientId': '<clientID>' }. But they are not available in the java file. I have no issues in iOS build.

There are no issues in android build process.

henritoivar commented 8 years ago

Hi. Having a similar issue on Android. I was going through the plugin code and did not see a place where the REVERSED_CLIENT_ID was used for Android. So is this even neccessary?

I have installed the plugin with the following command: cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-googleplus --variable REVERSED_CLIENT_ID="MY_CLIENT_ID"

First i use the following code to either display the google login button or not: $ionicPlatform.ready(function () { window.plugins.googleplus.isAvailable( function (available) { if (!available) { model.googleIsAvailable = false; } } ); });

When getting output from logcat i get the following when starting the app: run.txt

I use the following code to login: `window.plugins.googleplus.login({ webClientId: API.googleWebClientId }, function (success) {

  var params = {
    clientId: API.googleWebClientId
  };

  if(typeof success.serverAuthCode!== 'undefined'){
    params.code = success.serverAuthCode;
  }
  if(typeof success.idToken!== 'undefined'){
    params.access_token = success.idToken;
  }

  // Authenticate with our server
  $http.post(API.baseUrl + '/auth/google', params).then(function (response) {
    console.log(response);
    deferred.resolve(response);
  }, function (err) {
    console.log('error with linking google api call', err);
    deferred.reject(err);
  });

}, function (err) {
  console.log(err);
  deferred.reject(err);
  // throw err;
});`

The console outputs error with linking google api call 8 when trying to login

Logcat outputs the following: login.txt

sammugg commented 8 years ago

@henritoivar, Your issue is not related to this one. Please open a separate ticket. Thanks!

Fatal-Errol commented 8 years ago

I am having the same issue as this one. Same error message, parameters not empty but args.optJSONObject(0) is returning null so calling optString in a null object throws the error. Using latest versions and pulled code directly from git. It ran perfectly fine before but then I made a readded the plugins and this happened. @pranay-mathur were you able to resolve this?