def GetCredentials(self): if HAS_CRYPTO: if USE_NEW_OAUTH: return ServiceAccountCredentials._from_p12_keyfile_contents( self._client_id, self._private_key, private_key_password=self._password, scopes=DEFAULT_SCOPE) else: return SignedJwtAssertionCredentials( self._client_id, self._private_key, scope=DEFAULT_SCOPE, private_key_password=self._password) else: raise MissingDependencyError( 'Service account authentication requires PyOpenSSL. Please install ' 'this library and try again.')
def create_directory_service(): """Build and returns an Admin SDK Directory service object authorized with the service accounts that act on behalf of the given user. Args: user_email: The email of the user. Needs permissions to access the Admin APIs. Returns: Admin SDK directory service object. """ credentials = ServiceAccountCredentials._from_p12_keyfile_contents( SERVICE_ACCOUNT_EMAIL, SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret', scopes=['https://www.googleapis.com/auth/admin.directory.user',\ "https://www.googleapis.com/auth/admin.directory.group",\ "https://www.googleapis.com/auth/admin.directory.group.member",\ "https://www.googleapis.com/auth/admin.directory.user.alias",\ "https://www.googleapis.com/auth/admin.directory.userschema",\ "https://www.googleapis.com/auth/admin.directory.domain"]) credentials = credentials.create_delegated(USER_EMAIL) return build('admin', 'directory_v1', credentials=credentials)