gpt4 book ai didi

Java:谷歌日历 API

转载 作者:行者123 更新时间:2023-12-01 05:12:12 25 4
gpt4 key购买 nike

我正在使用 Google 日历 API。出于好奇,我没有使用 Google 库,所以我可以自己编写它。我无法制作新日历。身份验证正在工作,因为此获取请求正在工作: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list

为了制作新日历,我使用:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

我写道:

try {           
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,false);
HttpPost httppost = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList");
httppost.addHeader("Authorization", "Bearer " + token); //authentication
httppost.addHeader("Host", "googleapis.com");

StringEntity params = new StringEntity("{\"id\": \"TestSchedule\",\"defaultReminders\":[{\"method\": \"popup\",\"minutes\":10}]}");
httppost.setEntity(params);

//EXECUTE REQUEST
HttpResponse postResponse = httpclient.execute(httppost);

//RESPONSE
HttpEntity entity = postResponse.getEntity();
InputStream stream = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
httppost.releaseConnection();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

获取正确 token 的进一步范围是:https://www.googleapis.com/auth/calendar这应该是正确的。我认为问题出在我的帖子请求正文中?

因此,获取请求正在运行,因此我假设我的身份验证过程是正确的并且我得到了正确的 token 。执行此 post 请求而不是 get 请求结果是 302 响应代码。我得到的新URL是google的主页,根据引用页面我应该得到一个日历资源。

最佳答案

The following code working. please u can check it now. 



{
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,false);
HttpGet httpget = new HttpGet("https://www.googleapis.com/calendar/v3/users/me/calendarList");
httpget.addHeader("Authorization", "Bearer " + accessToken);

//HttpPost httppost = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList");
//httppost.addHeader("Authorization", "Bearer " + accessToken); //authentication
// httppost.addHeader("Host", "googleapis.com");

/* StringEntity params = new StringEntity("{\"id\":}");
httppost.setEntity(params);*/

//EXECUTE REQUEST
HttpResponse postResponse = httpclient.execute(httpget);

//RESPONSE
HttpEntity entity = postResponse.getEntity();
String responseBody1 = EntityUtils.toString(entity);
logger.debug(responseBody1);
}

关于Java:谷歌日历 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com