如何使用Google API插入新日历?

时间:2017-08-22 14:45:41

标签: java android google-api calendar

直升机,

我已获得以下代码:

            AccountManager accountmanager = AccountManager.get(Events.this.c);
            String SCOPE = "https://www.googleapis.com/auth/calendar";

            AccountManagerFuture<Bundle> authToken = accountmanager.getAuthToken(Events.this.account, "oauth2:" + SCOPE, null, Events.this.a,
                    null, null);
            String accessToken = authToken.getResult().getString(AccountManager.KEY_AUTHTOKEN);

            System.out.println("MYACCESSTOKEN: " + accessToken);

            url = new URL("https://www.googleapis.com/calendar/v3/calendars?key=...");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.addRequestProperty("client_id", "..." );
            conn.addRequestProperty("Authorization", "OAuth " + accessToken);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));

            writer.write("{\n" +
                    "   resource\n" +
                    "       {\n" +

                    "       \"summary\": \"Test\"\n" +
                    "       }\n" +
                    "}");

            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response="";

            }

获取accessstoken工作正常。

但是当我尝试插入新日历时,我收到400 HTTP错误。

我做错了什么?

密钥和oauth客户端ID绝对正确。我用......评论了它。

0 个答案:

没有答案