【发布时间】:2020-06-11 10:31:09
【问题描述】:
我有以下代码:
package com.example.bluelightlite.modules
import android.content.Context
import android.os.AsyncTask
import com.example.bluelightlite.builders.ServiceBuilder
import com.google.api.client.auth.oauth2.Credential
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.Calendar
import com.google.api.services.calendar.model.Event
import com.google.api.services.calendar.model.Events
class GoogleCalendarsServiceModule constructor(context: Context): AsyncTask<Void, Void, List<Event>>() {
private val googleCredential: Credential
private val httpTransport: NetHttpTransport = NetHttpTransport()
private val googleCredentialsUtilityModule = GoogleCredentialsUtilityModule(context)
init {
this.googleCredential = this.googleCredentialsUtilityModule.execute(this.httpTransport).get()
}
private var googleCalendarService: Calendar =
ServiceBuilder().buildGoogleCalendarService(this.googleCredential, this.httpTransport)
/**
* gets all calendar events
* @return Events list
*/
private fun getEvents(): List<Event> {
val now = DateTime(System.currentTimeMillis())
val events: Events = this.googleCalendarService
.events()
.list("Marc Freeman")
.setMaxResults(10)
.setOrderBy("startTime")
.execute()
return events.items;
}
override fun doInBackground(vararg params: Void?): List<Event> {
return this.getEvents()
}
}
应用程序要求用户接受访问日历的权限,我有一个身份验证令牌,但是当我调用时:
GoogleCalendarsServiceModule.execute() // because it needs to run on another thread
我得到这个错误:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.bluelightlite, PID: 11410
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:399)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.example.bluelightlite.modules.GoogleCalendarsServiceModule.getEvents(GoogleCalendarsServiceModule.kt:38)
at com.example.bluelightlite.modules.GoogleCalendarsServiceModule.doInBackground(GoogleCalendarsServiceModule.kt:44)
at com.example.bluelightlite.modules.GoogleCalendarsServiceModule.doInBackground(GoogleCalendarsServiceModule.kt:14)
at android.os.AsyncTask$3.call(AsyncTask.java:378)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
... 4 more
如何将我的访问令牌传递到我的请求中?当我调用 GoogleCalendarService 时,我似乎找不到允许我执行此操作的 API,顺便说一句,绝对是对 Android 的失望。
【问题讨论】:
-
您是否已将您的 credentials.json 添加到您的 Java 项目中? Follow this guide 让我知道这是否解决了您的问题。
-
老实说,我不知道发生了什么,但我清除了 build 文件夹并再次运行 dev build,它成功了...我现在将发布所有代码。
标签: android google-api google-calendar-api google-oauth google-api-java-client