def getCredentials(self, authorization_code=None): if self._credentials is None: db_settings = self.getDB().settings auth = db_settings.find_one( {'gdrive_credentials': { "$exists": True }}) if auth is not None: self._credentials = OAuth2Credentials.new_from_json( auth.get('gdrive_credentials')) else: auth = {} if self._credentials is not None and self._credentials.access_token_expired: self._credentials.refresh(httplib2.Http()) auth['gdrive_credentials'] = self._credentials.to_json() db_settings.save(auth) if self._credentials is None: try: self._credentials = self.exchange_code(authorization_code) except CodeExchangeException, error: error.authorization_url = self.get_authorization_url() raise error auth['gdrive_credentials'] = self._credentials.to_json() db_settings.save(auth)
def credentials_from_refresh_token(token): # why doesn't Google provide this!? cred_json = { "_module": "oauth2client.client", "token_expiry": "2000-01-01T00:13:37Z", # to refresh now "access_token": 'bogus', "token_uri": "https://accounts.google.com/o/oauth2/token", "invalid": False, "token_response": { "access_token": 'bogus', "token_type": "Bearer", "expires_in": 3600, "refresh_token": token }, "client_id": oauth.client_id, "id_token": None, "client_secret": oauth.client_secret, "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "_class": "OAuth2Credentials", "refresh_token": token, "user_agent": None } return OAuth2Credentials.new_from_json(json.dumps(cred_json))
def load_credentials(self, client_id, client_secret): app_root = os.path.realpath(os.path.join(__file__, '..', '..', '..')) credentials_file = os.path.join(app_root, 'data', 'credentials.json') try: with open(credentials_file, 'r') as f: credentials = OAuth2Credentials.new_from_json(f.read()) except IOError: flow = OAuth2WebServerFlow( client_id, client_secret, 'https://www.googleapis.com/auth/drive.readonly', 'urn:ietf:wg:oauth:2.0:oob', ) self._notice('Go to the following link in your browser: %s', flow.step1_get_authorize_url()) code = raw_input('Enter verification code: ').strip() credentials = flow.step2_exchange(code) with open(credentials_file, 'w') as f: f.write(credentials.to_json()) if not credentials: raise CommandError('Error load credentials') return credentials
def test_is_a_modifier_of_oauth2_credentials_for_a_data_source(self): model = DataSourceFactory(credentials=self.credentials()) storage = CredentialStorage(model) oauth2_credentials = self.oauth2_credentials() oauth2_credentials['refresh_token'] = 'new token' storage.locked_put( OAuth2Credentials.new_from_json(json.dumps(oauth2_credentials))) storage = storage.locked_get() assert_that(storage.refresh_token, equal_to('new token'))
def test_is_a_modifier_of_oauth2_credentials_for_a_data_source(self): model = DataSourceFactory(credentials=self.credentials()) storage = CredentialStorage(model) oauth2_credentials = self.oauth2_credentials() oauth2_credentials['refresh_token'] = 'new token' storage.locked_put( OAuth2Credentials.new_from_json(json.dumps(oauth2_credentials))) storage = storage.locked_get() assert_that(storage.refresh_token, equal_to('new token'))
def locked_get(self): """Retrieve credential. The Storage lock must be held when this is called. Returns: oauth2client.client.Credentials """ credential = OAuth2Credentials.new_from_json(self._user.auth) if credential: credential.set_store(self) return credential
def locked_get(self): """ Make an OAuth 2.0 credentials object from a JSON description of it stored in a model's credentials attribute. Returns: oauth2client.OAuth2Credentials """ db_credentials = json.loads(self.model.credentials) oauth2_credentials = OAuth2Credentials.new_from_json( json.dumps(db_credentials.get('OAUTH2_CREDENTIALS'))) oauth2_credentials.set_store(self) return oauth2_credentials
def locked_get(self): """ Make an OAuth 2.0 credentials object from a JSON description of it stored in a model's credentials attribute. Returns: oauth2client.OAuth2Credentials """ db_credentials = json.loads(self.model.credentials) oauth2_credentials = OAuth2Credentials.new_from_json( json.dumps(db_credentials.get('OAUTH2_CREDENTIALS'))) oauth2_credentials.set_store(self) return oauth2_credentials
def get_stored_credentials(self): fcred = self.get_fcred() if not os.path.exists(fcred): return None print 'open OAuth2 credential for %s CI=%s' % (self.oa2act, self.clientId) while True: pid = getpass.getpass() try: credentials = OAuth2Credentials.new_from_json( readJsonCredential(pid, fcred)) except (Exception, ), e: credentials = None if credentials is None or credentials.invalid: n = raw_input('password may be incorrect.\n %s\n %s\n number ?: ' % ( '1: re-enter password', '2: dispose and create new credential for %s' % self.oa2act)) if n == '2': break else: self.oa2act = credentials.token_response['id_token']['email'] break
def credentials_from_refresh_token(token): # why doesn't Google provide this!? cred_json = { "_module": "oauth2client.client", "token_expiry": "2000-01-01T00:13:37Z", # to refresh now "access_token": "bogus", "token_uri": "https://accounts.google.com/o/oauth2/token", "invalid": False, "token_response": {"access_token": "bogus", "token_type": "Bearer", "expires_in": 3600, "refresh_token": token}, "client_id": oauth.client_id, "id_token": None, "client_secret": oauth.client_secret, "revoke_uri": "https://accounts.google.com/o/oauth2/revoke", "_class": "OAuth2Credentials", "refresh_token": token, "user_agent": None, } return OAuth2Credentials.new_from_json(json.dumps(cred_json))
def get_stored_credentials(self): fcred = self.get_fcred() if not os.path.exists(fcred): return None print 'open OAuth2 credential for %s CI=%s' % (self.oa2act, self.clientId) while True: pid = getpass.getpass() try: credentials = OAuth2Credentials.new_from_json( readJsonCredential(pid, fcred)) except (Exception, ), e: credentials = None if credentials is None or credentials.invalid: n = raw_input( 'password may be incorrect.\n %s\n %s\n number ?: ' % ('1: re-enter password', '2: dispose and create new credential for %s' % self.oa2act)) if n == '2': break else: self.oa2act = credentials.token_response['id_token']['email'] break
def get_gmail_service(user_id): http = httplib2.Http() gmail_auth = Gmail.objects.get(user__id=user_id) credentials = OAuth2Credentials.new_from_json(gmail_auth.credentials_json) http = credentials.authorize(http) return build('gmail', 'v1', http=http)
def load_credentials(self): try: with open('credentials.json', 'r') as f: return OAuth2Credentials.new_from_json(f.read()) except IOError: return None
def load_credentials(self): try: with open('credentials.json', 'r') as f: return OAuth2Credentials.new_from_json(f.read()) except IOError: return None