4141import com .google .api .client .http .HttpStatusCodes ;
4242import com .google .api .client .json .JsonObjectParser ;
4343import com .google .api .client .util .GenericData ;
44+ import com .google .api .core .ObsoleteApi ;
4445import com .google .auth .CredentialTypeForMetrics ;
4546import com .google .auth .Credentials ;
4647import com .google .auth .Retryable ;
6970import java .util .Collections ;
7071import java .util .Date ;
7172import java .util .List ;
72- import java .util .Map ;
7373import java .util .Objects ;
7474import java .util .logging .Level ;
7575import java .util .logging .Logger ;
@@ -627,11 +627,22 @@ public static String getUniverseDomainUrl() {
627627 + "/computeMetadata/v1/universe/universe-domain" ;
628628 }
629629
630+ /**
631+ * This method is marked as Obsolete. Prefer to use {@link #getDefaultServiceAccount()} to
632+ * retrieve the default service account.
633+ */
634+ @ ObsoleteApi ("Prefer getDefaultServiceAccountUrl() to retrieve the default service account" )
630635 public static String getServiceAccountsUrl () {
631636 return getMetadataServerUrl (DefaultCredentialsProvider .DEFAULT )
632637 + "/computeMetadata/v1/instance/service-accounts/?recursive=true" ;
633638 }
634639
640+ /** Url to retrieve the default service account entry from the Metadata Server. */
641+ public static String getDefaultServiceAccountUrl () {
642+ return getMetadataServerUrl (DefaultCredentialsProvider .DEFAULT )
643+ + "/computeMetadata/v1/instance/service-accounts/default/email" ;
644+ }
645+
635646 public static String getIdentityDocumentUrl () {
636647 return getMetadataServerUrl (DefaultCredentialsProvider .DEFAULT )
637648 + "/computeMetadata/v1/instance/service-accounts/default/identity" ;
@@ -733,7 +744,7 @@ public byte[] sign(byte[] toSign) {
733744
734745 private String getDefaultServiceAccount () throws IOException {
735746 HttpResponse response =
736- getMetadataResponse (getServiceAccountsUrl (), RequestType .UNTRACKED , false );
747+ getMetadataResponse (getDefaultServiceAccountUrl (), RequestType .UNTRACKED , false );
737748 int statusCode = response .getStatusCode ();
738749 if (statusCode == HttpStatusCodes .STATUS_CODE_NOT_FOUND ) {
739750 throw new IOException (
@@ -756,12 +767,7 @@ private String getDefaultServiceAccount() throws IOException {
756767 // Mock transports will have success code with empty content by default.
757768 throw new IOException (METADATA_RESPONSE_EMPTY_CONTENT_ERROR_MESSAGE );
758769 }
759- GenericData responseData = response .parseAs (GenericData .class );
760- LoggingUtils .logResponsePayload (
761- responseData , LOGGER_PROVIDER , "Received default service account payload" );
762- Map <String , Object > defaultAccount =
763- OAuth2Utils .validateMap (responseData , "default" , PARSE_ERROR_ACCOUNT );
764- return OAuth2Utils .validateString (defaultAccount , "email" , PARSE_ERROR_ACCOUNT );
770+ return response .parseAsString ();
765771 }
766772
767773 public static class Builder extends GoogleCredentials .Builder {
0 commit comments