73
73
# The value representing service account credentials.
74
74
SERVICE_ACCOUNT = 'service_account'
75
75
76
- # The environment variable pointing the file with local Default Credentials.
77
- GOOGLE_CREDENTIALS_DEFAULT = 'GOOGLE_CREDENTIALS_DEFAULT'
76
+ # The environment variable pointing the file with local
77
+ # Application Default Credentials.
78
+ GOOGLE_APPLICATION_CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
78
79
79
80
# The access token along with the seconds in which it expires.
80
81
AccessTokenInfo = namedtuple ('AccessTokenInfo' , ['access_token' , 'expires_in' ])
@@ -111,8 +112,8 @@ class NonAsciiHeaderError(Error):
111
112
"""Header names and values must be ASCII strings."""
112
113
113
114
114
- class DefaultCredentialsError (Error ):
115
- """Error retrieving the Default Credentials."""
115
+ class ApplicationDefaultCredentialsError (Error ):
116
+ """Error retrieving the Application Default Credentials."""
116
117
117
118
118
119
def _abstract ():
@@ -903,14 +904,15 @@ def _get_environment(urllib2_urlopen=None):
903
904
904
905
905
906
class GoogleCredentials (OAuth2Credentials ):
906
- """Default credentials for use in calling Google APIs.
907
+ """Application Default Credentials for use in calling Google APIs.
907
908
908
- The Default Credentials are being constructed as a function of the environment
909
- where the code is being run. More details can be found on this page:
910
- https://developers.google.com/accounts/docs/default-credentials
909
+ The Application Default Credentials are being constructed as a function of
910
+ the environment where the code is being run.
911
+ More details can be found on this page:
912
+ https://developers.google.com/accounts/docs/application-default-credentials
911
913
912
- Here is an example of how to use the Default Credentials for a service that
913
- requires authentication:
914
+ Here is an example of how to use the Application Default Credentials for a
915
+ service that requires authentication:
914
916
915
917
<code>
916
918
from googleapiclient.discovery import build
@@ -919,7 +921,8 @@ class GoogleCredentials(OAuth2Credentials):
919
921
PROJECT = 'bamboo-machine-422' # replace this with one of your projects
920
922
ZONE = 'us-central1-a' # replace this with the zone you care about
921
923
922
- service = build('compute', 'v1', credentials=GoogleCredentials.get_default())
924
+ credentials = GoogleCredentials.get_application_default()
925
+ service = build('compute', 'v1', credentials=credentials)
923
926
924
927
request = service.instances().list(project=PROJECT, zone=ZONE)
925
928
response = request.execute()
@@ -949,7 +952,8 @@ def __init__(self, access_token, client_id, client_secret, refresh_token,
949
952
950
953
This constructor is not usually called by the user, instead
951
954
GoogleCredentials objects are instantiated by
952
- GoogleCredentials.from_stream() or GoogleCredentials.get_default().
955
+ GoogleCredentials.from_stream() or
956
+ GoogleCredentials.get_application_default().
953
957
954
958
Args:
955
959
access_token: string, access token.
@@ -982,53 +986,53 @@ def create_scoped(self, scopes):
982
986
return self
983
987
984
988
@staticmethod
985
- def get_default ():
986
- """Get the Default Credentials for the current environment.
989
+ def get_application_default ():
990
+ """Get the Application Default Credentials for the current environment.
987
991
988
992
Exceptions:
989
- DefaultCredentialsError: raised when the credentials fail to be retrieved.
993
+ ApplicationDefaultCredentialsError: raised when the credentials fail
994
+ to be retrieved.
990
995
"""
991
996
992
997
_env_name = _get_environment ()
993
998
994
999
if _env_name in ('GAE_PRODUCTION' , 'GAE_LOCAL' ):
995
1000
# if we are running inside Google App Engine
996
1001
# there is no need to look for credentials in local files
997
- default_credential_filename = None
1002
+ application_default_credential_filename = None
998
1003
well_known_file = None
999
1004
else :
1000
- default_credential_filename = _get_environment_variable_file ()
1005
+ application_default_credential_filename = _get_environment_variable_file ()
1001
1006
well_known_file = _get_well_known_file ()
1002
1007
1003
- if default_credential_filename :
1008
+ if application_default_credential_filename :
1004
1009
try :
1005
- return _get_default_credential_from_file (default_credential_filename )
1006
- except (DefaultCredentialsError , ValueError ) as error :
1007
- extra_help = (' (pointed to by ' + GOOGLE_CREDENTIALS_DEFAULT +
1010
+ return _get_application_default_credential_from_file (
1011
+ application_default_credential_filename )
1012
+ except (ApplicationDefaultCredentialsError , ValueError ) as error :
1013
+ extra_help = (' (pointed to by ' + GOOGLE_APPLICATION_CREDENTIALS +
1008
1014
' environment variable)' )
1009
- _raise_exception_for_reading_json (default_credential_filename ,
1010
- extra_help , error )
1015
+ _raise_exception_for_reading_json (
1016
+ application_default_credential_filename , extra_help , error )
1011
1017
elif well_known_file :
1012
1018
try :
1013
- return _get_default_credential_from_file (well_known_file )
1014
- except (DefaultCredentialsError , ValueError ) as error :
1019
+ return _get_application_default_credential_from_file (well_known_file )
1020
+ except (ApplicationDefaultCredentialsError , ValueError ) as error :
1015
1021
extra_help = (' (produced automatically when running'
1016
1022
' "gcloud auth login" command)' )
1017
1023
_raise_exception_for_reading_json (well_known_file , extra_help , error )
1018
1024
elif _env_name in ('GAE_PRODUCTION' , 'GAE_LOCAL' ):
1019
- return _get_default_credential_GAE ()
1025
+ return _get_application_default_credential_GAE ()
1020
1026
elif _env_name == 'GCE_PRODUCTION' :
1021
- return _get_default_credential_GCE ()
1027
+ return _get_application_default_credential_GCE ()
1022
1028
else :
1023
- raise DefaultCredentialsError (
1024
- "The Default Credentials are not available. They are available if "
1025
- "running in Google App Engine or Google Compute Engine. They are "
1026
- "also available if using the Google Cloud SDK and running 'gcloud "
1027
- "auth login'. Otherwise, the environment variable " +
1028
- GOOGLE_CREDENTIALS_DEFAULT + " must be defined pointing to a file "
1029
- "defining the credentials. "
1030
- "See https://developers.google.com/accounts/docs/default-credentials "
1031
- "for details." )
1029
+ raise ApplicationDefaultCredentialsError (
1030
+ "The Application Default Credentials are not available. They are "
1031
+ "available if running in Google Compute Engine. Otherwise, the "
1032
+ " environment variable " + GOOGLE_APPLICATION_CREDENTIALS +
1033
+ " must be defined pointing to a file defining the credentials. "
1034
+ "See https://developers.google.com/accounts/docs/application-default-"
1035
+ "credentials for more information." )
1032
1036
1033
1037
@staticmethod
1034
1038
def from_stream (credential_filename ):
@@ -1041,41 +1045,46 @@ def from_stream(credential_filename):
1041
1045
are to be read
1042
1046
1043
1047
Exceptions:
1044
- DefaultCredentialsError: raised when the credentials fail to be retrieved.
1048
+ ApplicationDefaultCredentialsError: raised when the credentials fail
1049
+ to be retrieved.
1045
1050
"""
1046
1051
1047
1052
if credential_filename and os .path .isfile (credential_filename ):
1048
1053
try :
1049
- return _get_default_credential_from_file (credential_filename )
1050
- except (DefaultCredentialsError , ValueError ) as error :
1054
+ return _get_application_default_credential_from_file (
1055
+ credential_filename )
1056
+ except (ApplicationDefaultCredentialsError , ValueError ) as error :
1051
1057
extra_help = ' (provided as parameter to the from_stream() method)'
1052
1058
_raise_exception_for_reading_json (credential_filename ,
1053
1059
extra_help ,
1054
1060
error )
1055
1061
else :
1056
- raise DefaultCredentialsError ('The parameter passed to the from_stream()'
1057
- ' method should point to a file.' )
1062
+ raise ApplicationDefaultCredentialsError (
1063
+ 'The parameter passed to the from_stream() '
1064
+ 'method should point to a file.' )
1058
1065
1059
1066
1060
1067
def _get_environment_variable_file ():
1061
- default_credential_filename = os .environ .get (GOOGLE_CREDENTIALS_DEFAULT ,
1062
- None )
1068
+ application_default_credential_filename = (
1069
+ os .environ .get (GOOGLE_APPLICATION_CREDENTIALS ,
1070
+ None ))
1063
1071
1064
- if default_credential_filename :
1065
- if os .path .isfile (default_credential_filename ):
1066
- return default_credential_filename
1072
+ if application_default_credential_filename :
1073
+ if os .path .isfile (application_default_credential_filename ):
1074
+ return application_default_credential_filename
1067
1075
else :
1068
- raise DefaultCredentialsError (
1069
- 'File ' + default_credential_filename + ' (pointed by ' +
1070
- GOOGLE_CREDENTIALS_DEFAULT + ' environment variable) does not exist!' )
1076
+ raise ApplicationDefaultCredentialsError (
1077
+ 'File ' + application_default_credential_filename + ' (pointed by ' +
1078
+ GOOGLE_APPLICATION_CREDENTIALS +
1079
+ ' environment variable) does not exist!' )
1071
1080
1072
1081
1073
1082
def _get_well_known_file ():
1074
1083
"""Get the well known file produced by command 'gcloud auth login'."""
1075
1084
# TODO(orestica): Revisit this method once gcloud provides a better way
1076
1085
# of pinpointing the exact location of the file.
1077
1086
1078
- WELL_KNOWN_CREDENTIALS_FILE = 'credentials_default .json'
1087
+ WELL_KNOWN_CREDENTIALS_FILE = 'application_default_credentials .json'
1079
1088
CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
1080
1089
1081
1090
if os .name == 'nt' :
@@ -1098,14 +1107,17 @@ def _get_well_known_file():
1098
1107
return default_config_path
1099
1108
1100
1109
1101
- def _get_default_credential_from_file (default_credential_filename ):
1102
- """Build the Default Credentials from file."""
1110
+ def _get_application_default_credential_from_file (
1111
+ application_default_credential_filename ):
1112
+ """Build the Application Default Credentials from file."""
1103
1113
1104
1114
import service_account
1105
1115
1106
1116
# read the credentials from the file
1107
- with open (default_credential_filename ) as default_credential :
1108
- client_credentials = service_account .simplejson .load (default_credential )
1117
+ with open (application_default_credential_filename ) as (
1118
+ application_default_credential ):
1119
+ client_credentials = service_account .simplejson .load (
1120
+ application_default_credential )
1109
1121
1110
1122
credentials_type = client_credentials .get ('type' )
1111
1123
if credentials_type == AUTHORIZED_USER :
@@ -1114,9 +1126,9 @@ def _get_default_credential_from_file(default_credential_filename):
1114
1126
required_fields = set (['client_id' , 'client_email' , 'private_key_id' ,
1115
1127
'private_key' ])
1116
1128
else :
1117
- raise DefaultCredentialsError ( "'type' field should be defined "
1118
- " (and have one of the '" + AUTHORIZED_USER +
1119
- "' or '" + SERVICE_ACCOUNT + "' values)" )
1129
+ raise ApplicationDefaultCredentialsError (
1130
+ "'type' field should be defined (and have one of the '" +
1131
+ AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)" )
1120
1132
1121
1133
missing_fields = required_fields .difference (client_credentials .keys ())
1122
1134
@@ -1142,25 +1154,25 @@ def _get_default_credential_from_file(default_credential_filename):
1142
1154
1143
1155
1144
1156
def _raise_exception_for_missing_fields (missing_fields ):
1145
- raise DefaultCredentialsError ( 'The following field(s): ' +
1146
- ', ' .join (missing_fields ) + ' must be defined.' )
1157
+ raise ApplicationDefaultCredentialsError (
1158
+ 'The following field(s) must be defined: ' + ', ' .join (missing_fields ))
1147
1159
1148
1160
1149
1161
def _raise_exception_for_reading_json (credential_file ,
1150
1162
extra_help ,
1151
1163
error ):
1152
- raise DefaultCredentialsError ( 'An error was encountered while reading '
1153
- ' json file: '+ credential_file + extra_help +
1154
- ': ' + str (error ))
1164
+ raise ApplicationDefaultCredentialsError (
1165
+ 'An error was encountered while reading json file: '+
1166
+ credential_file + extra_help + ': ' + str (error ))
1155
1167
1156
1168
1157
- def _get_default_credential_GAE ():
1169
+ def _get_application_default_credential_GAE ():
1158
1170
from oauth2client .appengine import AppAssertionCredentials
1159
1171
1160
1172
return AppAssertionCredentials ([])
1161
1173
1162
1174
1163
- def _get_default_credential_GCE ():
1175
+ def _get_application_default_credential_GCE ():
1164
1176
from oauth2client .gce import AppAssertionCredentials
1165
1177
1166
1178
return AppAssertionCredentials ([])
0 commit comments