[英]Reproducing code from Calendar API Explorer in Java
我正在使用API資源管理器: http : //developers.google.com/apis-explorer/#p/calendar/v3/calendar.calendarList.list
我可以得到該工作(在通過OAuth進行身份驗證之后)。 該請求看起來像這樣:
GET https://www.googleapis.com/calendar/v3/users/me/calendarList?fields=items(description%2Ckind%2Csummary)&key= {YOUR_API_KEY}
授權: Bearer ya29.1.FGDtN_WzfD1qNt86qJJlIwR65hdr1sU6BYqndDRPgyMF5xVK2olOacitZH41hQ
X-JavaScript-User-Agent: Google APIs Explorer
我正在嘗試使用Jersey重現它(鍵和東西已更改):
String apikey = "blah-blah-e2GB-yZp7CasdfoC8Bm10ZDhCpk";
String getCals = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
Client client = ClientBuilder.newClient();
WebTarget target = client.target(getCals);
target = target.queryParam("fields", "items(description%2Cid%2Ckind%2Csummary)");
target = target.queryParam("key", apikey);
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
builder.header("Authorization:", "Bearer ya29.1.AADtN_WzfDabsdwerwer0d0f0ndDRPgyMF5xVK2olOacitZH41hQ");
Response response = builder.get();
int status = response.getStatus();
String ret = response.readEntity(String.class);
log.debug("status: " + status + " response:\n" + ret);
不幸的是,這行不通-我回來了:
jerseyTest App - status: 401 response:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
誰能看到原因?
您沒有指定要為其列出日歷的用戶。 在兩足式OAuth中,您可以使用xoauth_requestor_id
參數指定用戶。 請參閱此鏈接Calendar API v3,兩足式OAuth和Java,以獲取更多信息。 但是,我認為這不是主要問題。 主要問題是您不能使用API Explorer中的Authorization
標頭。 您必須為您的API-key
和secret
生成一個新API-key
。 有關更多信息, 請參見本頁授權對Google Calendar API的請求 。 在Java中,您可以使用Google為Calendar API開發的非常好的庫: google-api-services-calendar 。 我建議使用此庫而不是生成本機查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.