def test_credentials_good(self): private_key = datafile('privatekey.pem') credentials = PyCryptoSignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') http = HttpMockSequence([ ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), ({'status': '200'}, 'echo_request_headers'), ]) http = credentials.authorize(http) resp, content = http.request('http://example.org') self.assertEqual('Bearer 1/3w', content['Authorization'])
def main(): key = open(KEY).read() credentials = PyCryptoSignedJwtAssertionCredentials( SERVICE_ACCOUNT, key, scope=" ".join(SCOPES), prn="*****@*****.**") http = httplib2.Http() httplib2.debuglevel = True http = credentials.authorize(http) service = build(serviceName='drive', version='v2', http=http) print service.files().list().execute()
def test_credentials_to_from_json(self): private_key = datafile('privatekey.pem') credentials = PyCryptoSignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') json = credentials.to_json() print json restored = Credentials.new_from_json(json) print restored.to_json() self.assertEqual(credentials.private_key, restored.private_key) self.assertEqual(credentials.private_key_password, restored.private_key_password) self.assertEqual(credentials.kwargs, restored.kwargs)
def test_credentials_refresh_with_storage(self): private_key = datafile('privatekey.pem') credentials = PyCryptoSignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') (filehandle, filename) = tempfile.mkstemp() os.close(filehandle) store = Storage(filename) store.put(credentials) credentials.set_store(store) content = self._credentials_refresh(credentials) self.assertEqual('Bearer 3/3w', content['Authorization']) os.unlink(filename)
def get(self): key = open(KEY).read() credentials = PyCryptoSignedJwtAssertionCredentials( SERVICE_ACCOUNT, key, scope=" ".join(SCOPES), prn="*****@*****.**") http = httplib2.Http() httplib2.debuglevel = 4 http = credentials.authorize(http) service = build(serviceName='drive', version='v2', http=http) docs = service.files().list().execute() self.response.out.write("<pre>") self.response.out.write("<h1>My Files...</h1>") for doc in docs["items"]: self.response.out.write("\t" + doc["title"] + "\n")
def test_credentials_refresh_without_storage(self): private_key = datafile('privatekey.pem') credentials = PyCryptoSignedJwtAssertionCredentials( '*****@*****.**', private_key, scope='read+write', prn='*****@*****.**') content = self._credentials_refresh(credentials) self.assertEqual('Bearer 3/3w', content["Authorization"])
## ## Services ## try: logger.debug("service init") key = open(crypto_key).read() logger.debug(">> loaded key") ## ## google oauth ## credentials = PyCryptoSignedJwtAssertionCredentials( service_account, key, scope=" ".join(gdata_scopes)) logger.debug(">> built credentials") http = httplib2.Http() httplib2.debuglevel = True http = credentials.authorize(http) logger.debug(">> authorized credentials and init'd http obj") service = build(serviceName='analytics', version='v3', http=http) logger.debug(">> analytics service init'd") ## ## twitter oauth ## auth = tweepy.OAuthHandler(tw_consumer_key, tw_consumer_secret)
logger.info("<<< Initializing Twitter Bot >>>") ## ## Services ## try: logger.debug("service init") key = open(crypto_key).read() logger.debug(">> loaded key") ## ## google oauth ## credentials = PyCryptoSignedJwtAssertionCredentials( service_account, key, scope=" ".join(gdata_scopes)) logger.debug(">> built credentials") http = httplib2.Http() httplib2.debuglevel = True http = credentials.authorize(http) logger.debug(">> authorized credentials and init'd http obj") service = build(serviceName='analytics', version='v3', http=http) logger.debug(">> analytics service init'd") ## ## twitter oauth ## auth = tweepy.OAuthHandler(tw_consumer_key, tw_consumer_secret) auth.set_access_token(tw_access_token, tw_access_token_secret)