Android日历api 403 forbidden权限不足错误

时间:2016-04-22 19:25:39

标签: android google-calendar-api android-calendar

我正在使用Google日历API开发日历应用。我可以查询谷歌日历中添加的事件列表,但是在向谷歌日历创建事件时我收到错误。以下是错误

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
   "errors" : [ {
     "domain" : "global",
     "message" : "Insufficient Permission",
     "reason" : "insufficientPermissions"
   } ],
   "message" : "Insufficient Permission"
 }

这是我的代码:

private void addEvent(String desc, String eventtime, Date date) {
    Event event = new Event()
            .setSummary(desc)
            .setDescription(desc);

    java.util.Calendar cal = java.util.Calendar.getInstance();
    TimeZone tz = cal.getTimeZone();
    DateTime startDateTime = new DateTime(new SimpleDateFormat("yyyy-MM-dd").format(date)+"T09:00:00-07:00");
    EventDateTime start = new EventDateTime()
            .setDateTime(startDateTime)
            .setTimeZone(String.valueOf(tz));
    event.setStart(start);

    DateTime endDateTime = new DateTime(new SimpleDateFormat("yyyy-MM-dd").format(date)+"T17:00:00-07:00");
    EventDateTime end = new EventDateTime()
            .setDateTime(endDateTime)
            .setTimeZone(String.valueOf(tz));
    event.setEnd(end);

    String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"};
    event.setRecurrence(Arrays.asList(recurrence));

    EventAttendee[] attendees = new EventAttendee[] {
            new EventAttendee().setEmail("someone@gmail.com"),
            new EventAttendee().setEmail("someother@gmail.com"),
    };
    event.setAttendees(Arrays.asList(attendees));

    EventReminder[] reminderOverrides = new EventReminder[] {
            new EventReminder().setMethod("email").setMinutes(24 * 60),
            new EventReminder().setMethod("popup").setMinutes(10),
    };
    Event.Reminders reminders = new Event.Reminders()
            .setUseDefault(false)
            .setOverrides(Arrays.asList(reminderOverrides));
    event.setReminders(reminders);

    new AddEventTask(event).execute();

}

AsyncClass

private class AddEventTask extends AsyncTask<Void, Void, Event>{

    private com.google.api.services.calendar.Calendar mService = null;
    private Exception mLastError = null;
    private Event event;

    public AddEventTask(Event event) {
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        mService = new com.google.api.services.calendar.Calendar.Builder(
                transport, jsonFactory, mCredential)
                .setApplicationName("Google Calendar API Android Quickstart")
                .build();
        this.event = event;
    }

    @Override
    protected Event doInBackground(Void... voids) {
        try {
            return addSingleEvent();
        } catch (Exception e) {
            mLastError = e;
            cancel(true);
            return null;
        }
    }

    private Event addSingleEvent() {

        String calendarId = "primary";
        try {
            event = mService.events().insert(calendarId, event).execute();
            System.out.printf("Event created: %s\n", event.getHtmlLink());
            return event;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    protected void onPreExecute() {
        mOutputText.setText("");
        mProgress.show();
    }

    @Override
    protected void onPostExecute(Event s) {
        super.onPostExecute(s);
        mProgress.hide();
        if (s == null ) {
            mOutputText.setText("No results returned.");
        } else {
            System.out.printf("Event created: %s\n", event.getHtmlLink());
            mOutputText.setText(s.getHtmlLink());
        }
    }

    @Override
    protected void onCancelled() {
        mProgress.hide();
        if (mLastError != null) {
            if (mLastError instanceof GooglePlayServicesAvailabilityIOException) {
                showGooglePlayServicesAvailabilityErrorDialog(
                        ((GooglePlayServicesAvailabilityIOException) mLastError)
                                .getConnectionStatusCode());
            } else if (mLastError instanceof UserRecoverableAuthIOException) {
                startActivityForResult(
                        ((UserRecoverableAuthIOException) mLastError).getIntent(),
                        CalendarActivity.REQUEST_AUTHORIZATION);
            } else {
                mOutputText.setText("The following error occurred:\n"
                        + mLastError.getMessage());
            }
        } else {
            mOutputText.setText("Request cancelled.");
        }
    }

}

需要帮助才能解决此问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

这是因为SCOPES中的权限不足。

添加以下代码以获得权限:

private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY ,CalendarScopes.CALENDAR};