android google calendar api not working when in release

纵饮孤独 提交于 2020-12-25 09:53:45

问题


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("public-calendar-id@group.calendar.google.com")
                                   .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("public-calendar-id@group.calendar.google.com")
.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.


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/35117723/android-google-calendar-api-not-working-when-in-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!