Google Sheets API access using Service Account from Android Device - android

Making app in Unity.
App works perfectly in Unity editor:
can access firebase for user login - this isnt the issue
can read and write from/to google sheet located in google drive using service account
When I build to android (apk file), and install on physical andoid device (Samsung S20 phone):
can access firebase for user login - so it is not an network/internet problem
CANT read/write from google sheet.
A portion of my code is here:
Authorisation - i am not sure if this bit works on the phone, or how to check.
public void Auth() // authorise access to the googlesheet online
{
//jsonKey = File.ReadAllText("Assets/Resources/Creds/beerhats-db-3***.json"); // location of key - read it - save to string
TextAsset txtAsset = (TextAsset)Resources.Load("beerhats-db-3***", typeof(TextAsset)); //i changed the file name here on stackoverflow just in case its a security thing
string jsonKey = txtAsset.text;
ServiceAccountCredential.Initializer initializer = new ServiceAccountCredential.Initializer(Secrets.serviceAccountID);
var jsonData = (JObject)JsonConvert.DeserializeObject(jsonKey);
if (jsonData != null) // the json file isnt moving or changing - so probably dont need an if statemet - remove later
{
string privateKey = jsonData["private_key"].Value<string>(); // specifically get the private key from all the data in the json file
ServiceAccountCredential credential = new ServiceAccountCredential(initializer.FromPrivateKey(privateKey));
service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
}
}
Part of my ReadFile code:
public async Task<string> ReadFile(string sheetName, string cellLocation) // for reading data only
{
// creates a string of location to write to
string whereToRead = sheetName + "!" + cellLocation;
SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(Secrets.spreadsheetID, whereToRead);
StringBuilder sb = new StringBuilder();
ValueRange response = await request.ExecuteAsync(); // run this on a separate thread as it takes long?
IList<IList<object>> values = response.Values;
On the phone, it executes the stringbuilder line, but not the executeasync() line.
Tried:
upgrade Unity version
change the min and target API levels
introduced keystore and project keys
introduced async incase it was a loading issue
change custom gradle template checkboxes
change read access on android sdk
update nuget installs of google apis
asking chatgpt for help
looking at various forums for similar issues
I am not running on an emulator, so no help with errors during run time. No errors in editor when built.
Expecting:
when installed on android device, the app to read/write from google sheet located in google drive
Requesting:
ideas to try to get this working! Thankyou :)

Related

AndroidRuntime: FATAL EXCEPTION: androidmapsapi-ZoomTableManager

My app that uses Google Maps SDK (v2) just started crashing with this exception:
Process: com.currentlocation.android, PID: 7328
java.lang.ArrayIndexOutOfBoundsException: length=1; index=12
at com.google.maps.api.android.lib6.gmm6.vector.ct.<init>(:com.google.android.gms.dynamite_mapsdynamite#201216081#20.12.16 (120400-0):9)
at com.google.maps.api.android.lib6.gmm6.vector.cv.a(:com.google.android.gms.dynamite_mapsdynamite#201216081#20.12.16 (120400-0):23)
at com.google.maps.api.android.lib6.gmm6.util.m.run(:com.google.android.gms.dynamite_mapsdynamite#201216081#20.12.16 (120400-0):14)
at java.lang.Thread.run(Thread.java:919)
In the v3 beta SDK, the stack trace is:
2020-04-23 15:59:06.064 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 22717
java.lang.ArrayIndexOutOfBoundsException: length=1; index=12
at com.google.android.libraries.maps.bv.zzbs.<init>(ZoomTable.java:24)
at com.google.android.libraries.maps.bv.zzbv.zza(ZoomTableQuadTree.java:57)
at com.google.android.libraries.maps.br.zzd.zza(Unknown Source:4)
at com.google.android.libraries.maps.hi.zzas.zza(Suppliers.java:7)
at com.google.android.libraries.maps.br.zza.zzh(SharedMapComponentImpl.java:58)
at com.google.android.libraries.maps.gu.zzat.zza(RendererFactoryImpl.java:88)
at com.google.android.libraries.maps.it.zzav.zza(GoogleMapImpl.java:59)
at com.google.android.libraries.maps.it.zzci.zza(MapFragmentDelegateImpl.java:3)
at com.google.android.libraries.maps.it.zzcg.zza(MapFragmentDelegateImpl.java:15)
at com.google.android.libraries.maps.SupportMapFragment$zza.onCreateView(SupportMapFragment.java:15)
at com.google.android.gms.dynamic.zae.zaa(com.google.android.gms:play-services-base##17.1.0:4)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaa(com.google.android.gms:play-services-base##17.1.0:9)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onCreateView(com.google.android.gms:play-services-base##17.1.0:25)
at com.google.android.libraries.maps.SupportMapFragment.onCreateView(SupportMapFragment.java:34)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:310)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2617)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:336)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1186)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2222)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1995)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1951)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2621)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:336)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1186)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2617)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2569)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1432)
at android.app.Activity.performStart(Activity.java:7848)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
2020-04-23 15:59:06.064 E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
What is the cause?
Edit: following is the official solution from Google (link)
Summary
Google Maps SDK thread crashes App (ArrayIndexOutOfBoundsException) - Solution Offered
Description
On April 23 2020 starting at 11:30 PDT, Google served for 4 hours an update to the configuration of a Maps mobile component, triggering crashes in Maps SDKs for Android and iOS. Applications on devices that downloaded this version of the configuration (during the outage period) were vulnerable to the crash. Workaround solutions are offered for Maps SDKs for Android and iOS.
Maps SDK for Android
Maps SDK for Android v2 (included in Google Play Services)
The updates to Google Play Services to fix the crash has been published to all devices with Google Play Services version 17.4.55 and newer. There is no change to the version number of Google Play Services on the device after the update is installed. No action is required from developers or end users to receive the updated Maps module; however, developers can verify that the module is present on a given device with the following adb command:
adb shell dumpsys activity provider com.google.android.gms.chimera.container.GmsModuleProvider
You should see the line Module Set ID: maps listed in the Module Sets section.
Module Set ID: maps, Module Set Version: 2015120015120000
The crash rates of Maps SDK for Android v2 are back to normal.
As of now, if you have not updated your app with the client-side code workarounds mentioned below, you do not need to take further action.
If you have already updated your app with the workarounds, you can remove the workaround in a subsequent update of your app (but keeping the workaround is safe).
Premium Plan Maps SDK for Android v2 or Maps SDK for Android v3 beta (static libraries)
If your app uses the Premium Plan Maps SDK for Android v2 or Maps SDK for Android v3 beta (static libraries), and is still experiencing crashes, we still highly recommend you to roll out the workarounds below via an update to your app. As your application is loading a static version of the SDK which is vulnerable to the bad data being stored on some devices, only an update to your application can solve the problem.
Play Store review approvals
If you update your app but experience Play Store review approvals delays, please file a support case with your app’s Package ID: ⁠Contact the support team. Our Support Team will internally escalate your request and expedite the approval.
Negative reviews in the Google Play Store
Some application developers inquired about 1-star reviews in the Google Play Store left by end-users due to crashes. Only comments that violate Google Play's policy [ 1 ] can be removed. You can also flag abusive reviews in the Play Console [ 2 ]. Applications will not be automatically removed from the Google Play store due to negative reviews. It's also worth noting that the calculation of your overall app review rating favors recent reviews, which means that your rating will recover to pre-incident levels over time.
[ 1 ] ⁠Ratings & Review on the Play Store
[ 2 ] ⁠Report inappropriate reviews
Maps SDK for iOS
Crash rates on iOS are back to normal. If your application is still experiencing crashes, you need to update and publish your app with the code workarounds communicated here.
For questions about deploying or expediting your application in the Apple App Store, please contact Apple directly.
With this update, we are closing this issue. Thank you to everyone for your patience. Our team is performing an in-depth internal investigation of this incident; as soon as possible, we will publish our analysis (in approximately a week). In the meantime, if you have any questions, or are still experiencing problems, please ⁠file a support case.
Workarounds:
End users on Android can clear the affected app's data (not just the
cache).
End users on iOS can uninstall then reinstall the affected app(s).
App Developers can apply the code workarounds below in order to solve
the issue for all their end users.
Code workaround for iOS:
Recommended placement for the code is before GMSServices initialization in the application(_:didFinishLaunchingWithOptions:) (Swift) or application:didFinishLaunchingWithOptions: (Objective-C) method. Specifically:
Swift:
let key = "GoogleMapsServerControlledParamsKey_bug_154855417"
if !UserDefaults.standard.bool(forKey: key) {
let urls = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if urls.count > 0 {
let paramUrl = urls[0].appendingPathComponent("com.google.GoogleMaps/ServerControlledParams", isDirectory: false)
try? FileManager.default.removeItem(at: paramUrl)
}
UserDefaults.standard.set(true, forKey: key)
}
Objective-C:
NSString *key = #"GoogleMapsServerControlledParamsKey_bug_154855417";
BOOL keyExists = [[NSUserDefaults standardUserDefaults] boolForKey:key];
if (!keyExists) {
NSArray<NSURL *> *array =
[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if (array.count > 0) {
NSURL *url =
[array[0] URLByAppendingPathComponent:#"com.google.GoogleMaps/ServerControlledParams"
isDirectory:NO];
if (url) {
[[NSFileManager defaultManager] removeItemAtURL:url error:NULL];
}
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
}
Code workaround for Android:
The recommended placement for the code is in Application.onCreate():
Java
try {
SharedPreferences hasFixedGoogleBug154855417 = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE);
if (!hasFixedGoogleBug154855417.contains("fixed")) {
File corruptedZoomTables = new File(getFilesDir(), "ZoomTables.data");
File corruptedSavedClientParameters = new File(getFilesDir(), "SavedClientParameters.data.cs");
File corruptedClientParametersData =
new File(
getFilesDir(),
"DATA_ServerControlledParametersManager.data."
+ getBaseContext().getPackageName());
File corruptedClientParametersDataV1 =
new File(
getFilesDir(),
"DATA_ServerControlledParametersManager.data.v1."
+ getBaseContext().getPackageName());
corruptedZoomTables.delete();
corruptedSavedClientParameters.delete();
corruptedClientParametersData.delete();
corruptedClientParametersDataV1.delete();
hasFixedGoogleBug154855417.edit().putBoolean("fixed", true).apply();
}
} catch (Exception e) {
}
Kotlin
try {
val sharedPreferences = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE)
if (!sharedPreferences.contains("fixed")) {
val corruptedZoomTables = File(filesDir, "ZoomTables.data")
val corruptedSavedClientParameters = File(filesDir, "SavedClientParameters.data.cs")
val corruptedClientParametersData = File(filesDir, "DATA_ServerControlledParametersManager.data.${packageName}")
val corruptedClientParametersDataV1 = File(filesDir, "DATA_ServerControlledParametersManager.data.v1.${packageName}")
corruptedZoomTables.delete()
corruptedSavedClientParameters.delete()
corruptedClientParametersData.delete()
corruptedClientParametersDataV1.delete()
sharedPreferences.edit().putBoolean("fixed", true).apply()
}
} catch (exception: Exception) {
}
The workarounds provided here covers all available flavors and versions of our SDKs for Android. To clarify further (in case you released an earlier version of the workaround that did not delete as many files):
Applications that use Maps Android SDK v2 should only need to delete
one file: ZoomTables.data.
Applications that use Maps Android SDK v3 beta should only need to
delete one file, either
DATA_ServerControlledParametersManager.data.v1. +
getBaseContext().getPackageName())
or
DATA_ServerControlledParametersManager.data. +
getBaseContext().getPackageName())
Seems in every app Google Map creates ZoomTables.data file.
This file was malformed, malformed version of it that could be downloaded from comment.
To reproduce the issue remove ZoomTables.data from the app packages on the device and insert malformed one. The app should crash.
Currently, the issue was solved on google's side, but the apps still contain a cached version of that data file.
To fix the issue we should remove that file right on the app start in Application onCreate Method.
private void fixGoogleMapBug() {
try {
SharedPreferences hasFixedGoogleBug154855417 = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE);
if (!hasFixedGoogleBug154855417.contains("fixed")) {
File corruptedZoomTables = new File(getFilesDir(), "ZoomTables.data");
File corruptedSavedClientParameters = new File(getFilesDir(), "SavedClientParameters.data.cs");
File corruptedClientParametersData =
new File(
getFilesDir(),
"DATA_ServerControlledParametersManager.data."
+ getBaseContext().getPackageName());
File corruptedClientParametersDataV1 =
new File(
getFilesDir(),
"DATA_ServerControlledParametersManager.data.v1."
+ getBaseContext().getPackageName());
corruptedZoomTables.delete();
corruptedSavedClientParameters.delete();
corruptedClientParametersData.delete();
corruptedClientParametersDataV1.delete();
hasFixedGoogleBug154855417.edit().putBoolean("fixed", true).apply();
}
} catch (Exception e) {
}
}
Update 1
I've updated workaround, base on the latest google developers comment:
The workarounds provided here covers all available flavors and versions of our SDKs for Android. To clarify further (in case you released an earlier version of the workaround that did not delete as many files):
Applications that use Maps Android SDK v2 should only need to delete one file: ZoomTables.data.
Applications that use Maps Android SDK v3 beta should only need to delete one file, either DATA_ServerControlledParametersManager.data.v1. + getBaseContext().getPackageName()) or DATA_ServerControlledParametersManager.data. + getBaseContext().getPackageName())
This solution worked for me
First open "App Info"
Choose "Storage"
Click "Clear data"
Open app again and check if the issue fixed.
Below are suggestions just to help your customers.
Send an email to your all users for the inconvenience and describe them the issue they faced and give them above mentioned steps to resolve their issue.
You can also send Push Notification to all your users with Firebase Push Notifications, if your app have the push notifications service.
Screenshots demonstration:
Drawbacks of solutions above:
ZoomTables.data is deleted on every device, regardless of whether the device is affected by the crash or not
Workaround only works once, is it sure that this problem never happens again?
Drawbacks of my solution:
on first execution of maps activity on affected device map is empty. After rotation of device or second execution map is shown up
My solution catches the Exception thrown by the Maps SDK, call this in onCreate of the Application class:
public static void catchGoogleMapsException(final Context context)
{
final Thread.UncaughtExceptionHandler defaultHandler =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(
(#NonNull final Thread thread, #NonNull final Throwable ex) ->
{
if (thread.getName().contains("ZoomTableManager"))
{
new File(context.getFilesDir(), "ZoomTables.data").delete();
Log.w("Maps Bug 154855417", "Caught exception and deleted ZoomTables.data");
}
else
if (defaultHandler!=null)
defaultHandler.uncaughtException(thread, ex);
else
throw new RuntimeException(
"No default uncaught exception handler.", ex);
});
}
Solution Offered for iOS & Android:
https://issuetracker.google.com/issues/154855417#comment509
Maps SDK for Android v2 (included in Google Play Services)
The updates to Google Play Services to fix the crash has been published to all devices with Google Play Services version 17.4.55 and newer. There is no change to the version number of Google Play Services on the device after the update is installed. No action is required from developers or end users to receive the updated Maps module; however, developers can verify that the module is present on a given device with the following adb command:
adb shell dumpsys activity provider com.google.android.gms.chimera.container.GmsModuleProvider
You should see the line Module Set ID: maps listed in the Module Sets section.
Module Set ID: maps, Module Set Version: 2015120015120000
The crash rates of Maps SDK for Android v2 are back to normal.
As of now, if you have not updated your app with the client-side code
workarounds mentioned below, you do not need to take further action.
If you have already updated your app with the workarounds, you can
remove the workaround in a subsequent update of your app (but keeping
the workaround is safe).
Code workaround for Android:
The productive version was fixed (on google's side) but if you still have problems with your emulator you have to run the following code only once.
The recommended placement for the code is in Application.onCreate():
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fixGoogleMapBugTemp() //TODO: Then clean this line
// ...
}
/**
* Observation: Invoke this method only once
*/
private fun fixGoogleMapBugTemp() {
val googleBug = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE)
if (!googleBug.contains("fixed")) {
val corruptedZoomTables = File(filesDir, "ZoomTables.data");
corruptedZoomTables.delete();
googleBug.edit().putBoolean("fixed", true).apply();
}
}
Workaround: Clear application data (not just the cache).
Note: A copy of the problematic file from the app packages on the device if anyone needs it for repro.
Source
GL
For Android, multiple developers mentioned a workaround consisting in deleting the ZoomTable.data file directly from their application. After review, this fix seems safe, and you could try it in your application.
Please refer to
https://issuetracker.google.com/154855417#comment179
If you want your users to continue using your app without re-installing,
The sample code is copy-pasted here for your convenience. In Application.onCreate():
SharedPreferences googleBug = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE);
if (!googleBug.contains("fixed")) {
File corruptedZoomTables = new File(getFilesDir(), "ZoomTables.data");
corruptedZoomTables.delete();
googleBug.edit().putBoolean("fixed", true).apply();
}
reference: Google Maps SDK is crashing -- partially resolved
Complete & official answer for all :
Diagnosis: Crash of the Google Maps Platform mobile SDKs (iOS & Android) at load.
Workaround: * Clear the affected app's data (not just the cache), or uninstall then reinstall the affected app(s).
Code workaround for iOS:
Recommended placement for the code is before GMSServices initialization in the application(_:didFinishLaunchingWithOptions:) (Swift) or application:didFinishLaunchingWithOptions: (Objective-C) method. Specifically:
Swift:
let key = "GoogleMapsServerControlledParamsKey_bug_154855417"
if !UserDefaults.standard.bool(forKey: key) {
let urls = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
if urls.count > 0 {
let paramUrl = urls[0].appendingPathComponent("com.google.GoogleMaps/ServerControlledParams", isDirectory: false)
try? FileManager.default.removeItem(at: paramUrl)
}
UserDefaults.standard.set(true, forKey: key)
}
Objective-C:
NSString *key = #"GoogleMapsServerControlledParamsKey_bug_154855417";
BOOL keyExists = [[NSUserDefaults standardUserDefaults] boolForKey:key];
if (!keyExists) {
NSArray<NSURL *> *array =
[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if (array.count > 0) {
NSURL *url =
[array[0] URLByAppendingPathComponent:#"com.google.GoogleMaps/ServerControlledParams"
isDirectory:NO];
if (url) {
[[NSFileManager defaultManager] removeItemAtURL:url error:NULL]);
}
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
}
Once you have deployed it in your app, you can file a Maps Support case if you would like us to help expedite its approval. Please make sure to include your application's ID, Bundle ID, and the version you want reviewed in your case.
Code workaround for Android:
The recommended placement for the code is in Application.onCreate():
Java
try {
SharedPreferences hasFixedGoogleBug154855417 = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE);
if (!hasFixedGoogleBug154855417.contains("fixed")) {
File corruptedZoomTables = new File(getFilesDir(), "ZoomTables.data");
File corruptedSavedClientParameters = new File(getFilesDir(), "SavedClientParameters.data.cs");
File corruptedClientParametersData =
new File(
getFilesDir(),
"DATA_ServerControlledParametersManager.data.v1."
+ getBaseContext().getPackageName());
corruptedZoomTables.delete();
corruptedSavedClientParameters.delete();
corruptedClientParametersData.delete();
hasFixedGoogleBug154855417.edit().putBoolean("fixed", true).apply();
}
} catch (Exception e) {
}
Kotlin
try {
val sharedPreferences = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE)
if (!sharedPreferences.contains("fixed")) {
val corruptedZoomTables = File(filesDir, "ZoomTables.data")
val corruptedSavedClientParameters = File(filesDir, "SavedClientParameters.data.cs")
val corruptedClientParametersData = File(filesDir, "DATA_ServerControlledParametersManager.data.v1.${packageName}")
corruptedZoomTables.delete()
corruptedSavedClientParameters.delete()
corruptedClientParametersData.delete()
sharedPreferences.edit().putBoolean("fixed", true).apply()
}
} catch (exception: Exception) {
}
Source : https://issuetracker.google.com/issues/1548554
You should call the follow method on the onCreate of your Android app to avoid the crash.
private fun clearCorruptedGMapsPreference() {
try {
val sharedPreferences = getSharedPreferences("google_bug_154855417", Context.MODE_PRIVATE)
if (!sharedPreferences.contains("fixed")) {
val corruptedZoomTables = File(filesDir, "ZoomTables.data")
val corruptedSavedClientParameters = File(filesDir, "SavedClientParameters.data.cs")
val corruptedClientParametersData = File(filesDir, "DATA_ServerControlledParametersManager.data.${packageName}")
val corruptedClientParametersDataV1 = File(filesDir, "DATA_ServerControlledParametersManager.data.v1.${packageName}")
corruptedZoomTables.delete()
corruptedSavedClientParameters.delete()
corruptedClientParametersData.delete()
corruptedClientParametersDataV1.delete()
sharedPreferences.edit().putBoolean("fixed", true).apply()
}
} catch (exception: Exception) {
LogUtil.e(this#CourierApplication::class.java.name, "An error has happened. Error: ".plus(exception.cause?.message
?: ""))
}
}
This is working code for Xamarin. You need to install Xamarin.Essentials nuget package.
if (!Preferences.Get("google_bug_fixed", false))
{
var corruptedZoomTables = new File(FileSystem.AppDataDirectory, "ZoomTables.data");
corruptedZoomTables.Delete();
Preferences.Set("google_bug_fixed", true);
}
Workaround suggested will only work one time:
I suggest to send a silent push notification to your app about Google Map Zoom Data Corrupted or make an API call to check the status of the Google Map Zoom Data. If you think this as un-necessary server call every time, you can either use push notification to trigger this logic or integrate with one of your existing calls where you check status of your servers. Leaving this to your app specific logics.Doing so, if Google Maps again throws this error you can re-trigger your work-around.
If server call, this will reset ZoomData for all your users.
If push-notifications, you can send notifications to some users.
fun receivedPushNotificationZoomDataCorrupted() {
try {
val corruptedZoomTables = File(filesDir, "ZoomTables.data")
val corruptedSavedClientParameters = File(filesDir, "SavedClientParameters.data.cs")
val corruptedClientParametersData = File(filesDir, "DATA_ServerControlledParametersManager.data.v1.${packageName}")
corruptedZoomTables.delete()
corruptedSavedClientParameters.delete()
corruptedClientParametersData.delete()
}
} catch (exception: Exception) {
}
}

Xamarin SqlServer cant get a connection

I'm building an app with the Entity Framework on Xamarin that lets me compare some data. But when I start my "fetchdata" function, I receive the Error:
System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)
I see many posts about Xamarin / Android & that it is not possible to get a connection to a SQL Server. Is there any way to fetch data from a SQL Server with .NET Core on Xamarin?
This is the string I put into SQL_Class folder with Sql_Common.cs
Fill up the brace brackets with actual parameters (removing the brace brakets too).
public static string SQL_connection_string = #"data source={server_address};initial catalog={database_name};user id={user_id};password={password};Connect Timeout={seconds}";
Then I access whenever I need it from any xamarin code just like we use in our asp.net c#
This works for me on my app without any issues.
using (SqlConnection Sql_Connection = new SqlConnection(Sql_Common.saralEHR_connection_string))
But as #Jason mentioned in his first reply, I too would get once again check the security part. I fexperienced before publishing Package to Google Play, they encrypt the App files with Hash Key Code and then only it gets upload to server
Yes it is possible (HuurrAYY!):
Im new in .net core, c# and so on and for me it was a hell of a work to get it working..
So here for the other noobs who are seeking for Help:
Guide´s i used:
Building Android Apps with Entity Framework
https://medium.com/#yostane/data-persistence-in-xamarin-using-entity-framework-core-e3a58bdee9d1
https://blog.xamarin.com/building-android-apps-entity-framework/
Scaffolding
https://cmatskas.com/scaffolding-dbcontext-and-models-with-entityframework-core-2-0-and-the-cli/
How i did it:
Build your normal Xamarin app.
create new .net solution like in the tutorials (DONT WRITE YOUR Entity Framework CLASSES)
create a third solution what has to be a .net core console application
Scaffold your DB in your CONSOLE application move all created classes & folders in your "xamarin .net" solution & change the namespaces
Ready to Go!
Side Node: NuGets you need in every solution:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
[EDIT: NuGets you need in every solution]
I am doing this way (working snippet):
string connectionString = #"data source={server};initial catalog={database};user id={user};password={password};Connect Timeout=10";
string databaseTable = "{table name}";
string selectQuery = String.Format("SELECT count(*) as Orders FROM {0}", databaseTable);
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//open connection
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
var result = command.ExecuteScalar().ToString();
//check if there is result
if(result != null)
{
OrdersLabel.Text = result;
}
}
}
catch (Exception ex)
{
OrdersLabel.Text = ex.Message;
}
It is working fine, but API call more elegant.
I hope it helps.

How to set android backend minSdk

im working on an android app with some server-side business logic. Im using android studio and im creating that kind of app for a first time.
I am trying to use server-side application to login to a different system and return me a cookie, so my android application can tell, whether the set credentials are correct.
Here's my endpoint provided method.
/** Returns user with cookie set either to null or actual cookie from AIS */
#ApiMethod(name = "login")
public User login(#Named("loginName") String name, #Named("password") String password) {
AISCommunicator aisCommunicator = new AISCommunicator();
String cookieVal = aisCommunicator.login(password,name);
User user = new User();
user.setCookie(cookieVal);
//user.setCookie("asdasdasd");
return user;
}
AISCommunicator is a serverside bean. At the moment it's part of a code
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
is marked as incorrect by Android studio, which tells me, that to use it, i need to declare minSdk level 9, while currently i have 1. How can i do that? I have set minSdk in my client's app, but it seems like it does not influence the serverside bean.
Anyway, the code is still runnable for some reason and the endpoint Bean returns 404 not found error at the moment.
Ignore Android Studio's error. This is one of its known and unfixed bugs.

Authenticating your client to Cloud Endpoints without a Google Account login

I have been doing extensive research on how to authenticate your client (Android, iOS, web-app) with Cloud Endpoints without requiring your user to use their Google account login the way the documentation shows you.
The reason for this is that I want to secure my API or "lock it down" to only my specified clients. Sometimes I will have an app that does not have a user login. I would hate to pester my user to now sign in just so my API is secure. Or other times, I just want to manage my own users like on a website and not use Google+, Facebook, or whatever else login authentication.
To start, let me first show the way you can authenticate your Android app with your Cloud Endpoints API using the Google Accounts login as specified in the documentation. After that I will show you my findings and a potential area for a solution which I need help with.
(1) Specify the client IDs (clientIds) of apps authorized to make requests to your API backend and (2) add a User parameter to all exposed methods to be protected by authorization.
public class Constants {
public static final String WEB_CLIENT_ID = "1-web-apps.apps.googleusercontent.com";
public static final String ANDROID_CLIENT_ID = "2-android-apps.googleusercontent.com";
public static final String IOS_CLIENT_ID = "3-ios-apps.googleusercontent.com";
public static final String ANDROID_AUDIENCE = WEB_CLIENT_ID;
public static final String EMAIL_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
}
import com.google.api.server.spi.auth.common.User; //import for the User object
#Api(name = "myApi", version = "v1",
namespace = #ApiNamespace(ownerDomain = "${endpointOwnerDomain}",
ownerName = "${endpointOwnerDomain}",
packagePath="${endpointPackagePath}"),
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,
Constants.IOS_CLIENT_ID,
Constants.API_EXPLORER_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
public class MyEndpoint {
/** A simple endpoint method that takes a name and says Hi back */
#ApiMethod(name = "sayHi")
public MyBean sayHi(#Named("name") String name, User user) throws UnauthorizedException {
if (user == null) throw new UnauthorizedException("User is Not Valid");
MyBean response = new MyBean();
response.setData("Hi, " + name);
return response;
}
}
(3) In Android call the API method in an Asynctask making sure to pass in the credential variable in the Builder:
class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;
#Override
protected String doInBackground(Pair<Context, String>... params) {
credential = GoogleAccountCredential.usingAudience(this,
"server:client_id:1-web-app.apps.googleusercontent.com");
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), credential)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://<your-app-engine-project-id-here>/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
#Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end options for devappserver
myApiService = builder.build();
}
context = params[0].first;
String name = params[0].second;
try {
return myApiService.sayHi(name).execute().getData();
} catch (IOException e) {
return e.getMessage();
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
What is happening is that in your Android app you are showing the Google account picker first, storing that Google account email in you shared preferences, and then later setting it as part of the GoogleAccountCredential object (more info on how to do that here).
The Google App Engine server receives your request and checks it. If the Android Client is one of the ones you specified in the #Api notation, then the server will inject the com.google.api.server.spi.auth.common.User object into your API method. It is now your responsibility to check if that User object is null or not inside your API method. If the User object is null, you should throw an exception in your method to prevent it from running. If you do not do this check, your API method will execute (a no-no if you are trying to restrict access to it).
You can get your ANDROID_CLIENT_ID by going to your Google Developers Console. There, you provide the package name of your Android App and the SHA1 which generates for you an android client id for you to use in your #Api annotation (or put it in a class Constants like specified above for usability).
I have done some extensive testing with all of the above and here is what I found:
If you specify a bogus or invalid Android clientId in your #Api annotation, the User object will be null in your API method. If you are doing a check for if (user == null) throw new UnauthorizedException("User is Not Valid"); then your API method will not run.
This is surprising because it appears there is some behind the scenes validation going on in Cloud Endpoints that check whether the Android ClientId is valid or not. If it is invalid, it won't return the User object - even if the end user logged in to their Google account and the GoogleAccountCredential was valid.
My question is, does anyone know how I can check for that type of ClientId validation on my own in my Cloud Endpoints methods? Could that information be passed around in an HttpHeader for example?
Another injected type in Cloud Endpoints is the javax.servlet.http.HttpServletRequest. You can get the request like this in your API method:
#ApiMethod(name = "sayHi")
public MyBean sayHi(#Named("name") String name, HttpServletRequest req) throws UnauthorizedException {
String Auth = req.getHeader("Authorization");//always null based on my tests
MyBean response = new MyBean();
response.setData("Hi, " + name);
return response;
}
}
But I am not sure if the necessary information is there or how to get it.
Certainly somewhere there must be some data that tells us if the Client is an authorized and specified one in the #Api clientIds.
This way, you could lock-down your API to your Android app (and potentially other clients) without ever having to pester your end users to log in (or just create your own simple username + password login).
For all of this to work though, you would have to pass in null in the third argument of your Builder like this:
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
Then in your API method extract whether or not the call came from an authenticated client, and either throw an exception or run whatever code you wanted to.
I know this is possible because when using a GoogleAccountCredential in the Builder, somehow Cloud Endpoints knows whether or not the call came from an authenticated client and then either injects its User object into the API method or not based on that.
Could that information be in the header or body somehow? If so, how can I get it out to later check if it is there or not in my API method?
Note: I read the other posts on this topic. They offer ways to pass in your own authentication token - which is fine - but your .apk will still not be secure if someone decompiles it. I think if my hypothesis works, you will be able to lock-down your Cloud Endpoints API to a client without any logins.
Custom Authentication for Google Cloud Endpoints (instead of OAuth2)
Authenticate my "app" to Google Cloud Endpoints not a "user"
Google Cloud Endpoints without Google Accounts
EDIT:
We used Gold Support for the Google Cloud Platform and have been talking back and forth with their support team for weeks. This is their final answer for us:
"Unfortunately, I haven't had any luck on this. I've asked around my
team, and checked all of the documentation. It looks like using OAuth2
is your only option. The reason is because the endpoint servers handle
the authentication before it reaches your app. This means you wouldn't
be able to develop your own authentication flow, and would get results
much like what you were seeing with the tokens.
I would be happy to submit a feature request for you. If you could
provide a little more information about why the OAuth2 flow doesn't
work for your customers, I can put the rest of the information
together and submit it to the product manager."
(frowny face) - however, maybe it is still possible?
I have implemented Endpoint Auth using a custom header "Authorization" and it works just fine. In my case this token is set after login but should work all the same with your app. Check your tests because the value should be there.
The way to retrieve that header is indeed:
String Auth = req.getHeader("Authorization");
You could take it a step further and define your own implementations of an Authenticator and apply it to your secure API calls.
So you don't have any user specific info, but just want to ensure that only your app is able to communicate with your backend...
This is what i think,
change
#Api(name = "myApi", version = "v1",
namespace = #ApiNamespace(ownerDomain = "${endpointOwnerDomain}",
ownerName = "${endpointOwnerDomain}",
packagePath="${endpointPackagePath}"),
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,
Constants.IOS_CLIENT_ID,
Constants.API_EXPLORER_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
{
...
}
to
#Api(name = "myApi", version = "v1",
namespace = #ApiNamespace(ownerDomain = "${endpointOwnerDomain}",
ownerName = "${endpointOwnerDomain}",
packagePath="${endpointPackagePath}"),
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.ANDROID_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
{
...
}
The Client ID is generated from the signature of your app. It can't be replicated. If you only allow your endpoints to accept requests from the Android App, your problem would be solved.
Tell me if this works.
Faced the same problem to find a solution to call my API safely from my endpoints, without using Google Account. We can't decompile an IOS App (Bundle), but decompile an Android App is so simple..
The solution I found is not perfect but do the job pretty good:
On android APP, I just create an constant String variable, named APIKey, with simply content (For example "helloworld145698")
Then I encrypt it with sha1, next md5, and finally sha1 (Order and frequency of encryption up to you) and store the variable on SharedPref (For Android) in private mode (Do this action on an random class in your App) It's this result encrypted I authorize on my Backend !
On my backend, I just add a parameter (named token for exemple) on every request
Example:
#ApiMethod(name = "sayHi")
public void sayHi(#Named("name") String name, #Named("Token") String token) {
if (token == tokenStoreOnAPIServer) {
//Allow it
} else {
//Refuse it and print error
}
}
On android, active ProGuard for obfuscated your code. It will be really unreadable for anyone who tried to decompile your app (Reverse engineering is really hardcore)
Not THE perfect secure solution, but it works, and it will be really really (really) difficult to find the real API key for anyone who try to read your code after decompilation.

Java based Google App Engine, Android and authentication oauth2

Authentication and app engine, there is a lot to be read about it, but a lot seems to be outdated!
Even the google pages https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls
Here, they talk about 'GoogleAccountCredential.usingAudience', but nowadays, you should use GoogleAuthUtil (as far as I know, please correct me if I'm wrong).
I am trying to set up an app engine as a backend to my Android app (and in future, my iOS app).
I am using Android Studio, used the 'new module' and chose app engine with cloud messaging there.
I created a simple endpoint, and have a function there, here is some code:
public class ReviewEndpoint {
// Make sure to add this endpoint to your web.xml file if this is a web application.
private static final Logger LOG = Logger.getLogger(ReviewEndpoint.class.getName());
/**
* This method gets the <code>Review</code> object associated with the specified <code>id</code>.
* #param id The id of the object to be returned.
* #return The <code>Review</code> associated with <code>id</code>.
*/
#ApiMethod(name = "getReview")
public Review getReview(#Named("id") Long id) {
// Implement this function
Review r = new Review();
r.setData("test!");
As you can see, this is nicely generated by Android Studio. I implemented some stuf like creating the 'review' object and return it at the end.
On the Android side, I can do this:
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
ReviewEndpoint ep = b.build();
Review review = ep.getReview(1L).execute();
data = review.getData();
and yes, I get 'test!' :)
Now, I want to have this authenticated. I want to know which user wrote what, so I thought I am going to use GMail account and Facebook later.
Here I'm stuck. I am able to get a token from the user on Android:
token = GoogleAuthUtil.getToken(MainScreenActivity.this, mAccount.name, "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile");
then you are able to add this token as credential to the request:
Credential cr = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(token);
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), cr);
Then in the app engine I tried to get the user info, but how?
Will it be supplied as 'bearer'? How do I get this bearer token? Should I then do API request to get the data on the server?
this does not work:
OAuthService service = OAuthServiceFactory.getOAuthService();
try {
User user = service.getCurrentUser();
can anyone give me a heads up?
So finally, today, I found out how to do it! I had questions on Stackoverflow on this before and never had an answer, but these to sites gave me the answer:
https://developers.google.com/appengine/docs/java/endpoints/auth
https://developers.google.com/appengine/docs/java/endpoints/consume_android
The first shows what needs to be done on the app engine side. The second page will tell you how to get the credentials. I was quite close. I am not sure if the adjusting of the build.gradle file mentioned in the second link is necessary. What I added to the App Engine:
#Api(name = "reviewEndpoint", version = "v1", ...<<some more stuff here >>
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE})
and then get the credentials:
// Initialize the scope using the client ID you got from the Console.
final String scope = "server:client_id:" + Constants.WEB_CLIENT_ID;
credential = GoogleAccountCredential.usingAudience(activity,scope);
You have to add the e-mail address of the user:
credential.setSelectedAccountName("some-mail-address#gmail.com");
you can get the e-mail address using the account picker (also example shown when you follow the link)
and next. you do a call to the endpoint, using the credential, I think Play Services will validate the user, because if I use an e-mail that is not logged in on the device, it will not work. The following code will throw an GoogleAuthIOException :
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), id_token);
ReviewEndpoint ep = b.build();
Review review;
review = ep.getReview(1L).execute();
for testing, I've put the e-mail address I get at the server side as a string in the review object, and there it gave me the e-mail address instead of the user object being null. Ow! I forgot to tell you, you need a user argument on the app engine side. Even though you do not see the 'user' argument in the 'getReview' call above, it will be added by App Engine.
So this is how my getReview looks now:
#ApiMethod(name = "getReview")
public Review getReview(#Named("id") Long id, User user) {
// Implement this function
Review r = new Review();
r.setData("user == " + (user == null ? "NULL " : user.toString()));
Hope this will help someone

Categories

Resources