GoogleSpeechTranscriptionService extends AbstractJobProducer implements TranscriptionService { protected String refreshAccessToken(String clientId, String clientSecret, String refreshToken) throws TranscriptionServiceException, IOException { CloseableHttpClient httpClient = makeHttpClient(); CloseableHttpResponse response = null; try { HttpPost httpPost = new HttpPost(tokenEndpoint + String.format( "?client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token", clientId, clientSecret, refreshToken)); httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); response = httpClient.execute(httpPost); int code = response.getStatusLine().getStatusCode(); String jsonString = EntityUtils.toString(response.getEntity()); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonString); switch (code) { case HttpStatus.SC_OK: accessToken = (String) jsonObject.get(ACCESS_TOKEN_NAME); long duration = (long) jsonObject.get(ACCESS_TOKEN_EXPIRY_NAME); tokenExpiryTime = (System.currentTimeMillis() + (duration * 1000)); if (!INVALID_TOKEN.equals(accessToken)) { logger.info("Google Cloud Service access token created"); return accessToken; } throw new TranscriptionServiceException( String.format("Created token is invalid. Status returned: %d", code), code); case HttpStatus.SC_BAD_REQUEST: case HttpStatus.SC_UNAUTHORIZED: String error = (String) jsonObject.get("error"); String errorDetails = (String) jsonObject.get("error_description"); logger.warn("Invalid argument returned, status: {}", code); logger.warn("Unable to refresh Google Cloud Serice token, error: {}, error details: {}", error, errorDetails); break; default: logger.warn("Invalid argument returned, status: {}", code); } throw new TranscriptionServiceException( String.format("Could not create Google access token. Status returned: %d", code), code); } catch (TranscriptionServiceException e) { throw e; } catch (Exception e) { logger.warn("Unable to generate access token for Google Cloud Services"); return INVALID_TOKEN; } finally { try { httpClient.close(); if (response != null) { response.close(); } } catch (IOException e) { } } } GoogleSpeechTranscriptionService(); void activate(ComponentContext cc); @Override Job startTranscription(String mpId, Track track, String language); @Override Job startTranscription(String mpId, Track track); @Override void transcriptionDone(String mpId, Object obj); @Override String getLanguage(); @Override void transcriptionError(String mpId, Object obj); String getTranscriptionResults(String jobId); @Override MediaPackageElement getGeneratedTranscription(String mpId, String jobId); String getTranscriptionStatus(String mpId); void setServiceRegistry(ServiceRegistry serviceRegistry); void setSecurityService(SecurityService securityService); void setUserDirectoryService(UserDirectoryService userDirectoryService); void setOrganizationDirectoryService(OrganizationDirectoryService organizationDirectoryService); void setSmtpService(SmtpService service); void setWorkspace(Workspace ws); void setWorkingFileRepository(WorkingFileRepository wfr); void setDatabase(TranscriptionDatabase service); void setAssetManager(AssetManager service); void setWorkflowService(WorkflowService service); static final String DEFAULT_WF_DEF; static final String ENABLED_CONFIG; static final String GOOGLE_SPEECH_LANGUAGE; static final String PROFANITY_FILTER; static final String WORKFLOW_CONFIG; static final String DISPATCH_WORKFLOW_INTERVAL_CONFIG; static final String COMPLETION_CHECK_BUFFER_CONFIG; static final String MAX_PROCESSING_TIME_CONFIG; static final String NOTIFICATION_EMAIL_CONFIG; static final String CLEANUP_RESULTS_DAYS_CONFIG; static final String GOOGLE_CLOUD_CLIENT_ID; static final String GOOGLE_CLOUD_CLIENT_SECRET; static final String GOOGLE_CLOUD_REFRESH_TOKEN; static final String GOOGLE_CLOUD_BUCKET; static final String GOOGLE_CLOUD_TOKEN_ENDPOINT_URL; static final String ENCODING_EXTENSION; }
@Test public void testRefreshAccessTokenURL() throws Exception { CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class); StatusLine status = EasyMock.createNiceMock(StatusLine.class); EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes(); EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_NOT_FOUND).anyTimes(); EasyMock.replay(response, status); Capture<HttpPost> capturedPost = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedPost))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); String token = service.refreshAccessToken(CLIENT_ID, CLIENT_SECRET, CLIENT_TOKEN); Assert.assertEquals("client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&refresh_token=" + CLIENT_TOKEN + "&grant_type=refresh_token", capturedPost.getValue().getURI().getQuery()); Assert.assertEquals("-1", token); }