2014-10-10 155 views
0

我想致電Google Calendar API App Engine Java端點。這是我的(不完整的)實用程序類,用於調用Google日曆。我正嘗試從已認證的端點檢索或創建特定於我的應用「Zeppa」的用戶Google日曆。我通過調用zeppaCalendarEntry(User)來執行此操作,其中user是來自經過驗證的調用的用戶實例。 API控制檯啓用了日曆API,client_secrets.json文件用於App Engine和服務帳戶。從App Engine端點(Java)訪問Google API

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

}

回答

0

如果check the docs recommended method連接到從應用程序引擎谷歌的API,你可以看到,它不使用類加載器的getResourceAsStream,它File對象上使用FileInputStream。嘗試使用它,看看它是否停止返回null。

相關問題