private final GoogleCredential getDefaultCredentialUnsynchronized( HttpTransport transport, JsonFactory jsonFactory) throws IOException { if (detectedEnvironment == null) { detectedEnvironment = detectEnvironment(transport); } switch (detectedEnvironment) { case ENVIRONMENT_VARIABLE: return getCredentialUsingEnvironmentVariable(transport, jsonFactory); case WELL_KNOWN_FILE: return getCredentialUsingWellKnownFile(transport, jsonFactory); case APP_ENGINE: return getAppEngineCredential(transport, jsonFactory); case CLOUD_SHELL: return getCloudShellCredential(jsonFactory); case COMPUTE_ENGINE: return getComputeCredential(transport, jsonFactory); default: return null; } }
private boolean runningUsingWellKnownFile() { File wellKnownFileLocation = getWellKnownCredentialsFile(); try { return fileExists(wellKnownFileLocation); } catch (AccessControlException expected) { // Exception querying file system is expected on App-Engine return false; } }
private final GoogleCredential getAppEngineCredential( HttpTransport transport, JsonFactory jsonFactory) throws IOException { Exception innerException = null; try { Class<?> credentialClass = forName(APP_ENGINE_CREDENTIAL_CLASS); Constructor<?> constructor = credentialClass .getConstructor(HttpTransport.class, JsonFactory.class); return (GoogleCredential) constructor.newInstance(transport, jsonFactory); } catch (ClassNotFoundException e) { innerException = e; } catch (NoSuchMethodException e) { innerException = e; } catch (InstantiationException e) { innerException = e; } catch (IllegalAccessException e) { innerException = e; } catch (InvocationTargetException e) { innerException = e; } throw OAuth2Utils.exceptionWithCause(new IOException(String.format( "Application Default Credentials failed to create the Google App Engine service account" + " credentials class %s. Check that the component 'google-api-client-appengine' is" + " deployed.", APP_ENGINE_CREDENTIAL_CLASS)), innerException); }
synchronized (this) { if (cachedCredential == null) { cachedCredential = getDefaultCredentialUnsynchronized(transport, jsonFactory);
/** * {@link Beta} <br/> * Returns the Application Default Credentials. * * <p>Returns the Application Default Credentials which are credentials that identify and * authorize the whole application. This is the built-in service account if running on Google * Compute Engine or the credentials file from the path in the environment variable * GOOGLE_APPLICATION_CREDENTIALS.</p> * * @param transport the transport for Http calls. * @param jsonFactory the factory for Json parsing and formatting. * @return the credential instance. * @throws IOException if the credential cannot be created in the current environment. */ @Beta public static GoogleCredential getApplicationDefault( HttpTransport transport, JsonFactory jsonFactory) throws IOException { Preconditions.checkNotNull(transport); Preconditions.checkNotNull(jsonFactory); return defaultCredentialProvider.getDefaultCredential(transport, jsonFactory); }
private boolean useGAEStandardAPI() { Class<?> systemPropertyClass = null; try { systemPropertyClass = forName("com.google.appengine.api.utils.SystemProperty"); } catch (ClassNotFoundException expected) {