从应用程序引擎端点(Java)访问Google API
我想调用Google Calendar API应用程序引擎Java端点。这是我用来调用Google日历的(不完整的)实用程序类。我正在尝试从经过身份验证的端点检索或创建特定于我的应用程序“Zeppa”的用户谷歌日历。我通过调用zeppaCalendarEntry(User)来实现这一点,其中User是经过身份验证的调用中的用户实例。API控制台打开了日历API,client_secrets.json文件用于应用程序引擎和服务帐户 获取客户端凭据时出错,返回空InputStream。任何对标准程序或修复程序的引用都会很好从应用程序引擎端点(Java)访问Google API,java,google-app-engine,calendar,Java,Google App Engine,Calendar,我想调用Google Calendar API应用程序引擎Java端点。这是我用来调用Google日历的(不完整的)实用程序类。我正在尝试从经过身份验证的端点检索或创建特定于我的应用程序“Zeppa”的用户谷歌日历。我通过调用zeppaCalendarEntry(User)来实现这一点,其中User是经过身份验证的调用中的用户实例。API控制台打开了日历API,client_secrets.json文件用于应用程序引擎和服务帐户 获取客户端凭据时出错,返回空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();
}
}
如果要从App Engine连接到Google API,可以看到它没有使用classloader的getResourceAsStream
,而是在文件
对象上使用FileInputStream
。尝试使用它,看看它是否停止返回null