Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android google calendar api not working when in release

I am using google calendar api to get events from a public calendar. In the google developer console I have created a service account key (json) which I use to setup the GoogleCredential in the android code as follows:

 AssetManager am = getAssets();
 InputStream inputStream = am.open("key-file-name.json");

 GoogleCredential credential = GoogleCredential.fromStream(inputStream);

credential =credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/calendar.readonly"));

Then I use this GoogleCredential to get the calendar object

Calendar client = new Calendar.Builder(AndroidHttp.newCompatibleTransport(),
                   new JacksonFactory(),
                   credential).setApplicationName("someAppName").build();

Then I get the next 5 events from this calendar

  com.google.api.services.calendar.model.Events nextEvent =
                           client.events().list("[email protected]")
                                   .setTimeMin(new DateTime(new java.util.Date(), java.util.TimeZone.getDefault()))
                                   .setMaxResults(5)
                                   .setOrderBy("startTime")
                                   .setSingleEvents(true)
                                   .setShowDeleted(false)
                                   .execute();

While this code works fine in debug when running in android studio, when I build for release (sign with keystore file) it does not work. It just returns the following exception:

com.google.a.a.c.b.c: 404 Not Found 3097-3187/com.news.apoelnews W/System.err: Not Found 3097-3187/com.news.apoelnews W/System.err:
at com.google.a.a.c.d.a.c.b(Unknown Source)

Please help!

UPDATE I have added the use of android API key in the code as follows:

com.google.api.services.calendar.model.Events nextEvent =
client.events().list("[email protected]")
.setTimeMin(new DateTime(new java.util.Date(), java.util.TimeZone.getDefault()))
.setMaxResults(5)
.setOrderBy("startTime")
.setSingleEvents(true)
.setShowDeleted(false)
.setKey("api-key-string_from_developer_console"))
.execute();

This causes the following exception:

W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden W/System.err: "code" : 403, W/System.err: "errors" : [ { W/System.err: "domain" : "usageLimits", W/System.err:
"message" : "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions

PLease note that the API keys are created using the debug and the release SHA-1.

like image 890
Louis Avatar asked Jan 31 '16 18:01

Louis


People also ask

Why is my Google Calendar not loading?

Clear cache As you use your calendar app on a daily basis, the app keeps storing cache files that might be a reason for your app to work properly. To clear your app cache go to Settings>Apps and Notifications>Google Calendar>App Info>Clear Data. The syncing service should run smoothly after that.

Why is my Android calendar not showing events?

First, try these common fixes If you're not connected, make sure that data or Wi-Fi is on, and that you're not in Airplane mode. Next, check your device's app store to make sure the Google Calendar app is up to date. To the left of the calendar's name, make sure the box is checked.

How do I refresh Google Calendar on Android?

Here's how to refresh the Google Calendar on your Android smartphone. Step 1: Launch the Google Calendar app. Step 2: Tap the menu icon at the top-right corner of the app. Step 3: Tap the Refresh option.


1 Answers

The problem was during the build in release the gradle option 'minifyenable true' was messing with the google api class names. So the solution is to include: -keep class com.google.api.** { *; } in the proguard

like image 104
Louis Avatar answered Nov 15 '22 00:11

Louis