def test_pickle_and_json_interop(self): # Write a file with a pickled OAuth2Credentials. access_token = 'foo' client_id = 'some_client_id' client_secret = 'cOuDdkfjxxnv+' refresh_token = '1/0/a.df219fjls0' token_expiry = datetime.datetime.utcnow() token_uri = 'https://www.google.com/accounts/o8/oauth2/token' user_agent = 'refresh_checker/1.0' credentials = OAuth2Credentials( access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent) f = open(FILENAME, 'w') pickle.dump(credentials, f) f.close() # Storage should be not be able to read that object, as the capability to # read and write credentials as pickled objects has been removed. s = Storage(FILENAME) read_credentials = s.get() self.assertEquals(None, read_credentials) # Now write it back out and confirm it has been rewritten as JSON s.put(credentials) f = file(FILENAME) data = simplejson.load(f) f.close() self.assertEquals(data['access_token'], 'foo') self.assertEquals(data['_class'], 'OAuth2Credentials') self.assertEquals(data['_module'], OAuth2Credentials.__module__)
def test_save_to_well_known_file_service_account(self): credential_file = datafile( os.path.join('gcloud', 'application_default_credentials.json')) credentials = _get_application_default_credential_from_file( credential_file) temp_credential_file = datafile( os.path.join('gcloud', 'temp_well_known_file_service_account.json')) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = simplejson.load(f) self.assertEqual('service_account', d['type']) self.assertEqual('123', d['client_id']) self.assertEqual('*****@*****.**', d['client_email']) self.assertEqual('ABCDEF', d['private_key_id']) os.remove(temp_credential_file)
def test_save_to_well_known_file_authorized_user(self): credentials_file = datafile( os.path.join('gcloud', 'application_default_credentials_authorized_user.json')) credentials = _get_application_default_credential_from_file( credentials_file) temp_credential_file = datafile( os.path.join('gcloud', 'temp_well_known_file_authorized_user.json')) save_to_well_known_file(credentials, temp_credential_file) with open(temp_credential_file) as f: d = simplejson.load(f) self.assertEqual('authorized_user', d['type']) self.assertEqual('123', d['client_id']) self.assertEqual('secret', d['client_secret']) self.assertEqual('alabalaportocala', d['refresh_token']) os.remove(temp_credential_file)
def create_fitbit_oauth_service(): #TODO: cache in memcache, like lib/oauth2client/clientsecrets.py try: fp = file('fitbit_secrets.json', 'r') try: json = simplejson.load(fp) finally: fp.close() except IOError: logging.error('Cannot find Fitbit service info') return None return OAuth1Service( consumer_key=json['consumer_key'], consumer_secret=json['consumer_secret'], name='fitbit', request_token_url='http://api.fitbit.com/oauth/request_token', authorize_url='http://www.fitbit.com/oauth/authorize', access_token_url='http://api.fitbit.com/oauth/access_token', base_url='http://api.fitbit.com')
def test_pickle_and_json_interop(self): # Write a file with a pickled OAuth2Credentials. credentials = self.create_test_credentials() f = open(FILENAME, 'w') pickle.dump(credentials, f) f.close() # Storage should be not be able to read that object, as the capability to # read and write credentials as pickled objects has been removed. s = file.Storage(FILENAME) read_credentials = s.get() self.assertEquals(None, read_credentials) # Now write it back out and confirm it has been rewritten as JSON s.put(credentials) f = open(FILENAME) data = simplejson.load(f) f.close() self.assertEquals(data['access_token'], 'foo') self.assertEquals(data['_class'], 'OAuth2Credentials') self.assertEquals(data['_module'], OAuth2Credentials.__module__)
def test_pickle_and_json_interop(self): # Write a file with a pickled OAuth2Credentials. credentials = self.create_test_credentials() f = open(FILENAME, 'w') pickle.dump(credentials, f) f.close() # Storage should be not be able to read that object, as the capability to # read and write credentials as pickled objects has been removed. s = file.Storage(FILENAME) read_credentials = s.get() self.assertEquals(None, read_credentials) # Now write it back out and confirm it has been rewritten as JSON s.put(credentials) f = open(FILENAME) data = simplejson.load(f) f.close() self.assertEquals(data['access_token'], 'foo') self.assertEquals(data['_class'], 'OAuth2Credentials') self.assertEquals(data['_module'], OAuth2Credentials.__module__)