从App Engine端点(Java)访问Google API

时间:2014-10-10 18:00:56

标签: java google-app-engine calendar

我想调用Google Calendar API App Engine Java端点。这是用于调用Google日历的我(不完整)实用程序类。我正在尝试检索或创建特定于我的应用程序Zeppa'的用户Google日历。来自经过身份验证的端点。我通过调用zeppaCalendarEntry(User)来执行此操作,其中user是来自经过身份验证的调用的用户实例。 API控制台启用了Calendar API,client_secrets.json文件用于App Engine和服务帐户。

Get Client Credential中发生错误,返回null InputStream。任何对标准程序或修正的引用都会很棒。

class ZeppaCalendarUtils {

private ZeppaCalendarUtils() {
}

/**
 * Global instance of the {@link DataStoreFactory}. The best practice is to
 * make it a single globally shared instance across your application.
 */
private static final AppEngineDataStoreFactory DATA_STORE_FACTORY = AppEngineDataStoreFactory
        .getDefaultInstance();

/** Global instance of the HTTP transport. */
static final HttpTransport HTTP_TRANSPORT = new UrlFetchTransport();

/** Global instance of the JSON factory. */
static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

private static GoogleClientSecrets clientSecrets = null;

/**
 * Creates a new Google Calendar for user
 * @param calendarClient
 * @return
 * @throws GeneralSecurityException
 * @throws IOException
 */
private static CalendarListEntry createZeppaCalendarEntry(
        Calendar calendarClient) throws GeneralSecurityException,
        IOException {

    CalendarListEntry content = new CalendarListEntry();
    content.setAccessRole(Constants.CALENDAR_ACCESS_ROLE);
    content.setBackgroundColor(Constants.CALENDAR_COLOR_BACKGROUND);
    content.setForegroundColor(Constants.CALENDAR_COLOR_FOREGROUND);
    content.setDefaultReminders(Constants.CALENDAR_DEFAULT_REMINDERS);
    content.setSelected(Constants.CALENDAR_SELECTED);
    content.setSummary("Zeppa");
    content.setId(Constants.CALENDAR_ID);

    CalendarListEntry result = calendarClient.calendarList()
            .insert(content).execute();

    return result;

}

/**
 * Retrieve or create a Users calendar
 * @param user
 * @return
 * @throws GeneralSecurityException
 * @throws IOException
 */
public static CalendarListEntry zeppaCalendarEntry(User user)
        throws GeneralSecurityException, IOException {

    Calendar calendarClient = loadCalendarClient(user.getUserId());
    CalendarListEntry result = calendarClient.calendarList()
            .get(Constants.CALENDAR_ID).execute();

    if (result == null) {
        result = createZeppaCalendarEntry(calendarClient);
    }

    return result;
}

/**
 * Retrieve a Client Secrets
 * @return
 * @throws IOException
 */
static GoogleClientSecrets getClientCredential() throws IOException {
    if (clientSecrets == null) {

        InputStream stream = ZeppaCalendarUtils.class
                .getResourceAsStream("client_secrets.json");

        Reader clientSecretReader = new InputStreamReader(stream);
        clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                clientSecretReader);
    }
    return clientSecrets;
}

/**
 * Generate the calendar client for calls to the API
 * @param userId
 * @return
 * @throws IOException
 */
private static Calendar loadCalendarClient(String userId)
        throws IOException {
    Credential credential = newAuthFlow().loadCredential(userId);
    return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .build();
}


/**
 * Create an auth flow
 * @return
 * @throws IOException
 */
private static GoogleAuthorizationCodeFlow newAuthFlow() throws IOException {
    return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
            JSON_FACTORY, getClientCredential(),
            Collections.singleton(CalendarScopes.CALENDAR))
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline").build();
}

}

1 个答案:

答案 0 :(得分:0)

如果您check the docs recommended method从App Engine连接到Google API,则可以看到它没有使用类加载器getResourceAsStream,它使用FileInputStreamFile对象上。尝试使用它,看看它是否停止返回null。