Android Google日历api在发布时不起作用
我正在使用Google Calendar API从公共日历中获取事件.在google开发人员控制台中,我创建了一个服务帐户密钥(json),该密钥用于在android代码中设置GoogleCredential,如下所示:
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"));
然后我使用此GoogleCredential来获取日历对象
Calendar client = new Calendar.Builder(AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
credential).setApplicationName("someAppName").build();
然后我会从该日历中获取接下来的5个事件
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();
虽然此代码在android studio中运行时在调试中可以正常工作,但是当我为发布而构建(使用密钥库文件签名)时,它不起作用.它只是返回以下异常:
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未找到3097-3187/com.news.apoelnewsW/System.err:找不到3097-3187/com.news.apoelnews W/System.err:
在com.google.a.a.c.d.a.c.b(未知来源)
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)
请帮助!
更新我在代码中添加了android API密钥的使用,如下所示:
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();
这会导致以下异常:
W/System.err:com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止的W/System.err:代码":403,W/System.err:错误":[W/System.err:域":"usageLimits",W/System.err:
"message":已配置每个IP或每个引用者的限制在您的API密钥上,并且请求不符合这些限制
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
请注意,API密钥是使用调试和版本SHA-1创建的.
PLease note that the API keys are created using the debug and the release SHA-1.
问题出在发布版本的构建过程中,gradle选项"minifyenable true"与Google api类名称混为一谈.因此,解决方案包括:-keep com.google.api.** {*;}在护卫者中
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