简体   繁体   English

从 App Engine Endpoints 访问 Google API (Java)

[英]Access Google API from App Engine Endpoints (Java)

I would like to make calls to the Google Calendar API App Engine Java Endpoints.我想调用 Google Calendar API App Engine Java Endpoints。 This is my (incomplete) utility class for calling the Google Calendar.这是我用于调用 Google 日历的(不完整的)实用程序类。 I am trying to retrieve or create the User's Google Calendar specific to my App 'Zeppa' from an authenticated endpoint.我正在尝试从经过身份验证的端点检索或创建特定于我的应用程序“Zeppa”的用户 Google 日历。 I do this by calling zeppaCalendarEntry(User) where user is the user instance from the authenticated call.我通过调用 zeppaCalendarEntry(User) 来做到这一点,其中 user 是来自经过身份验证的调用的用户实例。 The API console has the Calendar API on and the client_secrets.json file is for App Engine and Service Accounts. API 控制台启用了日历 API,并且 client_secrets.json 文件用于 App Engine 和服务帐户。

The error occurs in Get Client Credential, a null InputStream is returned.获取客户端凭据时发生错误,返回空的 InputStream。 Any references to standard procedure or fixes would be great.任何对标准程序或修复的引用都会很棒。

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();
    }

}

If you check the docs recommended method to connect to a Google API from App Engine, you can see that it doesn't use the classloader's getResourceAsStream , it uses a FileInputStream on a File object.如果您检查文档推荐的从 App Engine 连接到 Google API 的方法,您会发现它没有使用类加载器的getResourceAsStream ,而是在File对象上使用FileInputStream Try to use that instead and see if it stops returning null.尝试使用它,看看它是否停止返回 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM