@Override public ForApiBuilder fromFile(URL path) throws ConfigurationLoadException { return from(configHelper.fromFile(path), path.toString()); }
throw new ValidationException("Multiple OAuth formats set. Please specify either " + "a service account key file or a client ID and secret, not both.", this.configFilePath != null ? generateFilePathWarning("jsonKeyFilePath") : "."); + "If you do not have a client ID or secret, please create one in the API console: " + "https://console.developers.google.com/project", this.configFilePath != null ? generateFilePathWarning("clientId") : "."), "clientId"); + "If you do not have a client ID or secret, please create one in the API console: " + "https://console.developers.google.com/project", this.configFilePath != null ? generateFilePathWarning("clientSecret") : "."), "clientSecret"); + "It is required for offline credentials. If you need to create one, see the " + "GetRefreshToken example.", this.configFilePath != null ? generateFilePathWarning("refreshToken") : "."), "refreshToken");
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>refreshToken</li> * <li>clientId</li> * <li>clientSecret</li> * <li>jsonKeyFilePath</li> * </ul><br> * For example, the AdWords OAuth2 refresh token can be read from: * <code>api.adwords.refreshToken</code> * * @param config the configuration * @return Builder populated from the Configuration */ @Override public ForApiBuilder from(Configuration config) { this.refreshToken = config.getString(getPropertyKey("refreshToken"), null); this.clientId = config.getString(getPropertyKey("clientId"), null); this.clientSecret = config.getString(getPropertyKey("clientSecret"), null); this.jsonKeyFilePath = config.getString(getPropertyKey("jsonKeyFilePath"), null); this.serviceAccountUser = config.getString(getPropertyKey("serviceAccountUser"), null); return this; }
throw new ValidationException("Multiple OAuth formats set. Please specify either " + "a service account key file or a client ID and secret, not both.", this.configFilePath != null ? generateFilePathWarning("jsonKeyFilePath") : "."); + "If you do not have a client ID or secret, please create one in the API console: " + "https://console.developers.google.com/project", this.configFilePath != null ? generateFilePathWarning("clientId") : "."), "clientId"); + "If you do not have a client ID or secret, please create one in the API console: " + "https://console.developers.google.com/project", this.configFilePath != null ? generateFilePathWarning("clientSecret") : "."), "clientSecret"); + "It is required for offline credentials. If you need to create one, see the " + "GetRefreshToken example.", this.configFilePath != null ? generateFilePathWarning("refreshToken") : "."), "refreshToken");
new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .withTokenUrlServer(testHttpServer.getServerUrl()) .fromFile( OfflineCredentialsIntegrationTest.class.getResource("props/ads-test.properties")) .build();
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>refreshToken</li> * <li>clientId</li> * <li>clientSecret</li> * <li>jsonKeyFilePath</li> * </ul><br> * For example, the AdWords OAuth2 refresh token can be read from: * <code>api.adwords.refreshToken</code> * * @param config the configuration * @return Builder populated from the Configuration */ @Override public ForApiBuilder from(Configuration config) { this.refreshToken = config.getString(getPropertyKey("refreshToken"), null); this.clientId = config.getString(getPropertyKey("clientId"), null); this.clientSecret = config.getString(getPropertyKey("clientSecret"), null); this.jsonKeyFilePath = config.getString(getPropertyKey("jsonKeyFilePath"), null); this.serviceAccountUser = config.getString(getPropertyKey("serviceAccountUser"), null); return this; }
/** * Tests that the builder builds and gets an OAuth2 token correctly. */ @Test public void testGenerateCredential() throws Exception { testHttpServer.setMockResponseBody( "{\"access_token\" : \"accessToken\"," + "\"token_type\" : \"Bearer\"," + "\"expires_in\" : 3600," + "\"refresh_token\" : \"newRefreshToken\"}"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .withTokenUrlServer(testHttpServer.getServerUrl()) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); Credential credential = offlineCredentials.generateCredential(); assertTrue(testHttpServer.getLastRequestBody().contains("grant_type=refresh_token")); assertTrue(testHttpServer.getLastRequestBody().contains("refresh_token=refreshToken")); assertTrue(testHttpServer.getLastRequestBody().contains("client_id=clientId")); assertTrue(testHttpServer.getLastRequestBody().contains("client_secret=clientSecret")); assertEquals("accessToken", credential.getAccessToken()); assertEquals("newRefreshToken", credential.getRefreshToken()); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_adWordsServiceAccount() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); String jsonKeyFilePath = "someJsonKeyFilePath"; config.setProperty("api.adwords.jsonKeyFilePath", jsonKeyFilePath); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.ADWORDS) .from(config) .build(); assertEquals(jsonKeyFilePath, offlineCredentials.getJsonKeyFilePath()); assertNull("service account user should be null", offlineCredentials.getServiceAccountUser()); // Create another credential with the service account user set. String serviceAccountUser = "someUser@example.com"; offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.ADWORDS) .from(config) .withServiceAccountUser(serviceAccountUser) .build(); assertEquals(jsonKeyFilePath, offlineCredentials.getJsonKeyFilePath()); assertEquals(serviceAccountUser, offlineCredentials.getServiceAccountUser()); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_dfpServiceAccount() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); String jsonKeyFilePath = "someJsonKeyFilePath"; config.setProperty("api.admanager.jsonKeyFilePath", jsonKeyFilePath); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); assertEquals(jsonKeyFilePath, offlineCredentials.getJsonKeyFilePath()); assertNull("service account user should be null", offlineCredentials.getServiceAccountUser()); // Create another credential with the service account user set. String serviceAccountUser = "someUser@example.com"; offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .withServiceAccountUser(serviceAccountUser) .build(); assertEquals(jsonKeyFilePath, offlineCredentials.getJsonKeyFilePath()); assertEquals(serviceAccountUser, offlineCredentials.getServiceAccountUser()); }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>refreshToken</li> * <li>clientId</li> * <li>clientSecret</li> * <li>jsonKeyFilePath</li> * <li>serviceAccountUser</li> * </ul><br> * For example, the AdWords OAuth2 refresh token can be read from: * <code>api.adwords.refreshToken</code> * * @param config the configuration * @param filePath the file path of the configuration * @return Builder populated from the Configuration */ ForApiBuilder from(Configuration config, String filePath) { from(config); this.configFilePath = filePath; return this; }
/** * Reads properties from the provided {@link Configuration} object * <br><br> * Understands the following properties suffixes: * <br><br> * <ul> * <li>refreshToken</li> * <li>clientId</li> * <li>clientSecret</li> * <li>jsonKeyFilePath</li> * <li>serviceAccountUser</li> * </ul><br> * For example, the AdWords OAuth2 refresh token can be read from: * <code>api.adwords.refreshToken</code> * * @param config the configuration * @param filePath the file path of the configuration * @return Builder populated from the Configuration */ ForApiBuilder from(Configuration config, String filePath) { from(config); this.configFilePath = filePath; return this; }
/** * Tests generating OAuth2 credentials. */ @Test public void testGenerateCredential() throws Exception { HttpTransport httpTransport = new NetHttpTransport(); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .withHttpTransport(httpTransport) .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())).thenReturn(true); Credential credential = offlineCredentials.generateCredential(); assertEquals( "clientId", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientId()); assertEquals( "clientSecret", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientSecret()); assertEquals("refreshToken", credential.getRefreshToken()); assertSame(httpTransport, credential.getTransport()); }
/** * Tests generating OAuth2 credentials. */ @Test public void testGenerateCredential_defaultTransport() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())).thenReturn(true); Credential credential = offlineCredentials.generateCredential(); assertEquals( "clientId", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientId()); assertEquals( "clientSecret", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientSecret()); assertEquals("refreshToken", credential.getRefreshToken()); assertSame(ForApiBuilder.DEFAULT_HTTP_TRANSPORT, credential.getTransport()); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_properPrefix() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientIdDfp"); config.setProperty("api.admanager.clientSecret", "clientSecretDfp"); config.setProperty("api.admanager.refreshToken", "refreshTokenDfp"); config.setProperty("api.adwords.clientId", "clientIdAdWords"); config.setProperty("api.adwords.clientSecret", "clientSecretAdWords"); config.setProperty("api.adwords.refreshToken", "refreshTokenAdWords"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); assertEquals("clientIdDfp", offlineCredentials.getClientId()); assertEquals("clientSecretDfp", offlineCredentials.getClientSecret()); assertEquals("refreshTokenDfp", offlineCredentials.getRefreshToken()); }
/** * Tests that the builder correctly reads from a file. */ @Test public void testReadPropertiesFromFile_withOverride() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); OfflineCredentials offlineCredentials = builder.fromFile("path").withRefreshToken("overrideRefreshToken").build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("overrideRefreshToken", offlineCredentials.getRefreshToken()); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientIdTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "Client ID must be set as api.admanager.clientId in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_refreshTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "A refresh token must be set as api.admanager.refreshToken in /home/user/path." + "\nIt is required for offline credentials. If you need to create one, " + "see the GetRefreshToken example."); builder.fromFile("/home/user/path").build(); }
/** * Tests that the builder correctly fails on a bad configuration that has serviceAccountUser * specified but does not have the jsonKeyFilePath. */ @Test public void testReadPropertiesFromConfiguration_serviceAccountUserNoKeyFile() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.clientSecret", "clientSecret"); config.setProperty("api.admanager.refreshToken", "refreshToken"); config.setProperty("api.admanager.serviceAccountUser", "someUser@example.com"); thrown.expect(ValidationException.class); new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.AD_MANAGER) .from(config) .build(); }
/** * Tests that the builder correctly reads properties from a configuration. */ @Test public void testReadPropertiesFromConfiguration_adwords() throws ValidationException { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.adwords.clientId", "clientId"); config.setProperty("api.adwords.clientSecret", "clientSecret"); config.setProperty("api.adwords.refreshToken", "refreshToken"); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder() .forApi(OfflineCredentials.Api.ADWORDS) .from(config) .build(); assertEquals("clientId", offlineCredentials.getClientId()); assertEquals("clientSecret", offlineCredentials.getClientSecret()); assertEquals("refreshToken", offlineCredentials.getRefreshToken()); }
/** * Tests that the builder correctly identifies the file path. */ @Test public void testReadPropertiesFromFile_clientSecretTokenBadWithFilePath() throws Exception { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty("api.admanager.clientId", "clientId"); config.setProperty("api.admanager.refreshToken", "refreshToken"); when(configurationHelper.fromFile("/home/user/path")).thenReturn(config); ForApiBuilder builder = new OfflineCredentials.ForApiBuilder( configurationHelper, OfflineCredentials.Api.AD_MANAGER, oAuth2Helper); thrown.expect(ValidationException.class); thrown.expectMessage( "Client secret must be set as api.admanager.clientSecret in /home/user/path." + "\nIf you do not have a client ID or secret, please create one in the API " + "console: https://console.developers.google.com/project"); builder.fromFile("/home/user/path").build(); }