6
votes

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.

3
Since you're using Service Accounts, are you using Calendar for Work ( Google Apps for Work)?adjuremods
No I don't..........................Louis
Why use Service Accounts? From what I know, you can use Service Accounts if you're using Google Apps for Work (domain-wide authority delegation).adjuremods
You are right. I modified my code now and I only use the android API key.Louis
add your domain name at developer consoleraj

3 Answers

10
votes

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

0
votes

Has the api/Key generated for the Signed Keystore, and have u changed the api key in ur project which matches your release Keystore

0
votes

i am not much aware about Google Calendar Api but Have tried to generate SHA-1 key with your release key-store? if you want to generate release apk then you have to create new key for Google Calendar and release again it will work for you.

follow this some useful links : https://developer.android.com/studio/publish/app-signing.html#releasemode

https://coderwall.com/p/r09hoq/android-generate-release-debug-keystores

try it