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.