def get_or_create_credentials_for(self, identifier, type, write=True, check_valid=True):
        try:
            creds = self.get_credentials_for(identifier, check_valid=False)
        except NameError, ne:
            creds = self.new_credentials_for(identifier, type)
            if write == True:
                creds.write()
        if check_valid == True and creds.valid() == False:
            # If it's an Oauth token maybe it needs refreshed?
            if isinstance(creds, OAuthCredentials):
                try:
                    creds.refreshToken()
                    if creds.valid():
                        if write == True:
                            creds.write()
                        return creds
                except NotImplementedError, nie:
                    pass
            # Lets give them 3 attempts to login
            for attempt_numer in range(3):
                creds.new_credentials()
                if creds.valid():
                    if write == True:
                        creds.write()
                    return creds
            # All attempts failed, return creds are bad
            raise CredentialsInvalidError("Invalid Credentials for %s" % identifier)
        return creds
dataset/ETHPy150Open SalesforceEng/Providence/Empire/creds/__init__.py/CredentialManager.get_or_create_credentials_for/original