def test_publish_notifications(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) credentials = self.mox.CreateMock(GoogleCredentials) GoogleCredentials.from_stream( _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials) credentials.create_scoped_required().AndReturn(True) credentials.create_scoped( pubsub_utils.PUBSUB_SCOPES).AndReturn(credentials) self.mox.StubOutWithMock(discovery, 'build') msg = _create_sample_message() discovery.build( pubsub_utils.PUBSUB_SERVICE_NAME, pubsub_utils.PUBSUB_VERSION, credentials=credentials ).AndReturn( MockedPubSub( self, 'test_topic', msg, pubsub_utils.DEFAULT_PUBSUB_NUM_RETRIES, # use tuple ('123') instead of list just for easy to # write the test. ret_val={'messageIds': ('123')})) self.mox.ReplayAll() pubsub_client = pubsub_utils.PubSubClient( _TEST_CLOUD_SERVICE_ACCOUNT_FILE) msg_ids = pubsub_client.publish_notifications('test_topic', [msg]) self.assertEquals(('123'), msg_ids) self.mox.VerifyAll()
def test_pubsub_with_corrupted_service_account(self): """Test pubsub with corrupted service account.""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(_TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) GoogleCredentials.from_stream( _TEST_CLOUD_SERVICE_ACCOUNT_FILE).AndRaise( ApplicationDefaultCredentialsError()) self.mox.ReplayAll() with self.assertRaises(pubsub_utils.PubSubException): pubsub_utils.PubSubClient(_TEST_CLOUD_SERVICE_ACCOUNT_FILE) self.mox.VerifyAll()
def test_get_pubsub_service_with_invalid_service_account(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) credentials = self.mox.CreateMock(GoogleCredentials) GoogleCredentials.from_stream( pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndRaise( ApplicationDefaultCredentialsError()) self.mox.ReplayAll() pubsub = pubsub_utils._get_pubsub_service() self.assertIsNone(pubsub) self.mox.VerifyAll()
def call_cloud_eval_with_model(X, MODEL_NAME): PROJECT_ID = "ehrkeras" CREDENTIALS_FILE = dirpath + "/lib/assets/credentials.json" list1 = [] for x in X: list1.append(x) inputs_for_prediction = [ {"input": list1} ] # Connect to the Google Cloud-ML Service credentials = GoogleCredentials.from_stream(CREDENTIALS_FILE) service = googleapiclient.discovery.build('ml', 'v1', credentials=credentials) # Connect to our Prediction Model name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME) response = service.projects().predict( name=name, body={'instances': inputs_for_prediction} ).execute() if 'error' in response: raise RuntimeError(response['error']) results = response['predictions'] return results
def GetService(self): credentials = GoogleCredentials.from_stream( self.credentials._credentials_file_path) service = googleapiclient.discovery.build("ml", "v1", credentials=credentials) return service
def get_storage_resource(): try: credentials = GoogleCredentials.get_application_default() except: # todo: replace filename with variable credentials = GoogleCredentials.from_stream("privatekey.json").create_scoped(STORAGE_SCOPES) return discovery.build('storage', 'v1', credentials=credentials)
def get_storage_resource(): credentials = GoogleCredentials.from_stream( settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(STORAGE_SCOPES) return discovery.build('storage', 'v1', credentials=credentials, cache_discovery=False)
def get_vision_service(): credentials = GoogleCredentials.from_stream( './application_default_credentials.json') return discovery.build('vision', 'v1', credentials=credentials, discoveryServiceUrl=DISCOVERY_URL)
def connect(project_id, instance_id, database_id, credentials_json=None, debug=False): if not credentials_json: if ON_GAE: auth_token, _ = app_identity.get_access_token( 'https://www.googleapis.com/auth/cloud-platform') else: raise RuntimeError( "You must specify the path to the credentials file") else: from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.from_stream(credentials_json) credentials = credentials.create_scoped( 'https://www.googleapis.com/auth/cloud-platform') access_token_info = credentials.get_access_token() auth_token = access_token_info.access_token return Connection(project_id, instance_id, database_id, auth_token, debug=debug)
def __init__(self, json_key_service_filename, project_name, bucket_name): """Creates the service object for calling the Cloud Storage API.""" credentials = GoogleCredentials.from_stream(json_key_service_filename) # Create connection self.client = storage.Client(project=project_name, credentials=credentials) self.bucketname = bucket_name
def __init__(self, discovery_file=None, api_version='v1', max_results=100, num_retries=3, handle_annotations='prefix'): if discovery_file is None: if 'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ: raise ValueError("No Google application credentials found. " "A JSON service account key must be either " "passed as the discovery_file argument, or " "set in the GOOGLE_APPLICATION_CREDENTIALS " "environment variable.") discovery_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] self.credentials = GoogleCredentials.from_stream(discovery_file) self.max_results = max_results self.num_retries = num_retries self.service = discovery.build(self.api_name, api_version, credentials=self.credentials, discoveryServiceUrl=DISCOVERY_URL) self.handle_annotations = handle_annotations super(GoogleAPITransformer, self).__init__()
def __init__(self): credentials = GoogleCredentials.from_stream(SpreadsheetComm.CLIENT_SECRET_FILE) credentials = credentials.create_scoped(SpreadsheetComm.SCOPES) conn = credentials.authorize(httplib2.Http()) self.service = discovery.build('sheets', 'v4', http=conn, discoveryServiceUrl=SpreadsheetComm.DISCOVERY_URL)
def __init__(self, project, bucket_name, region, package_path=PACKAGE_PATH, job_dir=JOB_DIR, http=None, credentials_path=None): self.project = project self.parent = "projects/{}".format(self.project) self.bucket_name = bucket_name self.region = region self.package_full_path = "gs://{}/{}".format(self.bucket_name, package_path) self.job_dir_suffix = "gs://{}/{}".format(self.bucket_name, job_dir) self.__logger = logging.getLogger(name=self.__class__.__name__) credentials = (None if not credentials_path else GoogleCredentials.from_stream(credentials_path)) self.client = discovery.build('ml', 'v1', http=http, credentials=credentials)
def start_gsuite_ingestion(neo4j_session: neo4j.Session, config: Config) -> None: """ Starts the GSuite ingestion process by initializing :param neo4j_session: The Neo4j session :param config: A `cartography.config` object :return: Nothing """ common_job_parameters = { "UPDATE_TAG": config.update_tag, } try: credentials = GoogleCredentials.from_stream(GSUITE_CREDS) credentials = credentials.create_scoped(OAUTH_SCOPE) credentials = credentials.create_delegated(GSUITE_DELEGATED_ADMIN) except ApplicationDefaultCredentialsError as e: logger.debug('Error occurred calling GoogleCredentials.get_application_default().', exc_info=True) logger.error( ( "Unable to initialize GSuite creds. If you don't have GSuite data or don't want to load " 'Gsuite data then you can ignore this message. Otherwise, the error code is: %s ' 'Make sure your GSuite credentials are configured correctly, your credentials file (if any) is valid. ' 'For more details see README' ), e, ) return resources = _initialize_resources(credentials) api.sync_gsuite_users(neo4j_session, resources.admin, config.update_tag, common_job_parameters) api.sync_gsuite_groups(neo4j_session, resources.admin, config.update_tag, common_job_parameters)
def get_speech_service(): """Make a speech service that we can use to make requests.""" credentials = GoogleCredentials.from_stream(SVC_PATH).create_scoped( ['https://www.googleapis.com/auth/cloud-platform']) http = httplib2.Http() credentials.authorize(http) return discovery.build('speech', 'v1beta1', http=http)
def get_credentials(creds): """ Function get_credentials """ credentials = GoogleCredentials.from_stream(creds) return credentials
def get_bigquery_service(): credentials = GoogleCredentials.from_stream(settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(BIGQUERY_SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = discovery.build('bigquery', 'v2', http=http) return service
def create(self, scopes): with open(self.path, 'r') as fh: data = json.load(fh) if data.get('type', None): credentials = GoogleCredentials.from_stream(self.path) credentials = credentials.create_scoped(scopes) return credentials return Storage(self.path).get()
def _MakeCreds(creds_path): """Creates a GoogleCredentials object with the trace.append scope. Args: creds_path: Path to the credentials file to use. """ return GoogleCredentials.from_stream( os.path.expanduser(creds_path)).create_scoped(SCOPES)
def get_gce_service(service_key): """Returns GCE compute resource object for interacting with GCE API :param service_key: string, Path of service key obtained from https://console.cloud.google.com/apis/credentials """ credentials = GoogleCredentials.from_stream(service_key) service = build('compute', 'v1', credentials=credentials) return service
def get_speech_service(): credentials = GoogleCredentials.from_stream( 'api/googleapi_auth/LecRec-a4f4c7931558.json').create_scoped( ['https://www.googleapis.com/auth/cloud-platform']) http = httplib2.Http() credentials.authorize(http) return discovery.build('speech', 'v1beta1', http=http)
def get_bigquery_service(): credentials = GoogleCredentials.from_stream(settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(BIGQUERY_SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = discovery.build('bigquery', 'v2', http=http, cache_discovery=False) return service
def __init__(self, table_key="1cbDYsZ0TESPJRtnJzwA78oLn6WDhgcAXOB8PjCKeNos"): self.path = os.path.dirname(os.path.realpath(__file__)) self.key = table_key # json_key = simplejson.load(open('ps4sales-aef32dacd287.json')) scope = ['https://spreadsheets.google.com/feeds'] credentials = GoogleCredentials.from_stream(self.path + '/ps4sales-aef32dacd287.json').create_scoped(scope) gc = gspread.authorize(credentials) self.wks = gc.open_by_key(self.key) # psn accounts return
def get_speech_service(): authfilename = 'Lecrec-kujyp-478172a2b31d' credentials = GoogleCredentials.from_stream( 'api/googleapi_auth/' + authfilename + '.json').create_scoped( ['https://www.googleapis.com/auth/cloud-platform']) http = httplib2.Http() credentials.authorize(http) return discovery.build('speech', 'v1beta1', http=http)
def authorize_credentials_with_google_from_file(credentials_path): if debug: print >> sys.stderr,'Called '+sys._getframe().f_code.co_name # documentation: https://developers.google.com/accounts/docs/application-default-credentials SCOPES = ['https://www.googleapis.com/auth/bigquery'] credentials = GoogleCredentials.from_stream(credentials_path).create_scoped(SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = build('bigquery', 'v2', http=http) return service
def get_gce_service(service_key): """Returns GCE compute resource object for interacting with GCE API :param service_key: string, Path of service key obtained from https://console.cloud.google.com/apis/credentials :return: :class:`Resource <Resource>` object :rtype: googleapiclient.discovery.Resource """ credentials = GoogleCredentials.from_stream(service_key) service = build('compute', 'v1', credentials=credentials) return service
def test_get_pubsub_service_with_invalid_service_account(self): """Test getting the pubsub service""" self.mox.StubOutWithMock(os.path, 'isfile') self.mox.StubOutWithMock(GoogleCredentials, 'from_stream') os.path.isfile(pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(True) credentials = self.mox.CreateMock(GoogleCredentials) GoogleCredentials.from_stream( pubsub_utils.CLOUD_SERVICE_ACCOUNT_FILE).AndReturn(credentials) credentials.create_scoped_required().AndReturn(True) credentials.create_scoped(pubsub_utils.PUBSUB_SCOPES).AndReturn( credentials) self.mox.StubOutWithMock(discovery, 'build') discovery.build(pubsub_utils.PUBSUB_SERVICE_NAME, pubsub_utils.PUBSUB_VERSION, credentials=credentials).AndRaise(UnknownApiNameOrVersion()) self.mox.ReplayAll() pubsub = pubsub_utils._get_pubsub_service() self.assertIsNone(pubsub) self.mox.VerifyAll()
def __init__(self, project_id, dataset_id, table_name, credentials_path): self.project_id = project_id self.dataset_id = dataset_id self.table_name = table_name self.credentials = GoogleCredentials.from_stream(credentials_path) self.bigquery = discovery.build('bigquery', 'v2', credentials=self.credentials)
def authorize_credentials_with_Google(): if settings.DEBUG: logger.debug('Called '+sys._getframe().f_code.co_name) # documentation: https://developers.google.com/accounts/docs/application-default-credentials SCOPES = ['https://www.googleapis.com/auth/bigquery'] # credentials = GoogleCredentials.get_application_default().create_scoped(SCOPES) credentials = GoogleCredentials.from_stream(settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = discovery.build('bigquery', 'v2', http=http, cache_discovery=False) if settings.DEBUG: logger.debug(' big query authorization '+sys._getframe().f_code.co_name) return service
def _load_credentials(cls, credentials_file_path): if credentials_file_path == GCE_CREDENTIALS: return AppAssertionCredentials(cls._SCOPES) with open(credentials_file_path, 'r') as credentials_file: credentials_json = json.load(credentials_file) if credentials_json.get('type', None): credentials = GoogleCredentials.from_stream(credentials_file_path) credentials = credentials.create_scoped(cls._SCOPES) return credentials return Storage(credentials_file_path).get()
def _load_credentials(cls, credentials_file_path): if credentials_file_path == GCE_CREDENTIALS: return AppAssertionCredentials(cls._SCOPES) with open(credentials_file_path, "r") as credentials_file: credentials_json = json.load(credentials_file) if credentials_json.get("type", None): credentials = GoogleCredentials.from_stream(credentials_file_path) credentials = credentials.create_scoped(cls._SCOPES) return credentials return Storage(credentials_file_path).get()
def authorize_credentials_with_Google(): if debug: print >> sys.stderr,'Called '+sys._getframe().f_code.co_name # documentation: https://developers.google.com/accounts/docs/application-default-credentials SCOPES = ['https://www.googleapis.com/auth/bigquery'] # credentials = GoogleCredentials.get_application_default().create_scoped(SCOPES) credentials = GoogleCredentials.from_stream(settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = build('bigquery', 'v2', http=http) if debug: print >> sys.stderr,' big query authorization '+sys._getframe().f_code.co_name return service
def __init__(self, project_id, bucket_name, credential_file=None): BaseTransfer.__init__(self) self.project_id = project_id if credential_file: creds = GoogleCredentials.from_stream(credential_file) else: creds = GoogleCredentials.get_application_default() gs = build("storage", "v1", credentials=creds) self.gs_buckets = gs.buckets() # pylint: disable=no-member self.gs_objects = gs.objects() # pylint: disable=no-member self.bucket_name = self.get_or_create_bucket(bucket_name) self.log.debug("GoogleTransfer initialized")
def get_storage_resource(perms=None): # credentials = GoogleCredentials.get_application_default() # todo: if perms == 'r', STORAGE_SCOPES[:1] # if perms == 'rw', STORAGE_SCOPES[:2] # credentials = GoogleCredentials.from_stream(settings.GOOGLE_APPLICATION_CREDENTIALS)\ .create_scoped(STORAGE_SCOPES) # http = httplib2.Http() # http = credentials.authorize(http) # print credentials.to_json() # return discovery.build('storage', 'v1', http=http) return discovery.build('storage', 'v1', credentials=credentials)
def get_speech_service(): """Make a speech service that we can use to make requests. This is lifted from the API examples provided here: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/api-client/transcribe_async.py#L35 """ credentials = GoogleCredentials.from_stream( settings.GOOGLE_AUTH["PATH"], ).create_scoped( ["https://www.googleapis.com/auth/cloud-platform"], ) http = httplib2.Http() credentials.authorize(http) return discovery.build("speech", "v1beta1", http=http)
def get_sheet_service(): SHEETS_SCOPES = [ 'https://www.googleapis.com/auth/spreadsheets' ] credentials = GoogleCredentials.from_stream( settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(SHEETS_SCOPES) http = httplib2.Http() http = credentials.authorize(http) service = build_with_retries('sheets', 'v4', None, 2, http=http) return service
def __init__(self, project, region_name, creds_path=None, verbose=False): """ Initialize the class """ if not creds_path: credentials = GoogleCredentials.get_application_default() else: credentials = GoogleCredentials.from_stream(creds_path) self._credentials = credentials self.scope = build('compute', 'beta', credentials=self._credentials) self.project = project self.region_name = region_name self._region = None self.verbose = verbose
def get_instances(credentials_file, project_id): credentials = GoogleCredentials.from_stream(credentials_file) compute = build('compute', 'v1', credentials=credentials) instances = [] for zone in ['us-east1-{}'.format(az) for az in 'bcd']: query = compute.instances().list(project=project_id, zone=zone) result = query.execute() zone_instances = result.get('items', []) instances.extend(zone_instances) return instances
def get_iam_resource(): """Returns an Identity Access Management service client for calling the API. """ IAM_SCOPES = [ 'https://www.googleapis.com/auth/iam', 'https://www.googleapis.com/auth/cloud-platform' ] credentials = GoogleCredentials.from_stream( settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(IAM_SCOPES) http = httplib2.Http() http = credentials.authorize(http) return discovery.build('iam', 'v1', http=http)
def _load_credentials(self, credentials_file_path): if credentials_file_path == GCE_CREDENTIALS: return gce.AppAssertionCredentials(self._SCOPES) if credentials_file_path == APPENGINE_CREDENTIALS: # pragma: no cover # This import doesn't work outside appengine, so delay it until it's used. from oauth2client import appengine return appengine.AppAssertionCredentials(self._SCOPES) with open(credentials_file_path, 'r') as credentials_file: credentials_json = json.load(credentials_file) if credentials_json.get('type', None): credentials = GoogleCredentials.from_stream(credentials_file_path) credentials = credentials.create_scoped(self._SCOPES) return credentials return Storage(credentials_file_path).get()
def ForServiceAccountThreadSafe(cls, project, zone, json_key_file): """Creates a thread-safe GceContext using service account credentials. About service account: https://developers.google.com/api-client-library/python/auth/service-accounts Args: project: The GCP project to create images and instances in. zone: The default zone to create instances in. json_key_file: Path to the service account JSON key. Returns: GceContext. """ credentials = GoogleCredentials.from_stream(json_key_file).create_scoped( cls._GCE_SCOPES) return GceContext(project, zone, credentials, thread_safe=True)
def _load_credentials(self, credentials_file_path): if credentials_file_path == GCE_CREDENTIALS: return gce.AppAssertionCredentials(self._SCOPES) if credentials_file_path == APPENGINE_CREDENTIALS: # pragma: no cover # This import doesn't work outside appengine, so delay it until it's used. from oauth2client import appengine from google.appengine.api import app_identity logging.info("Initializing with service account %s", app_identity.get_service_account_name()) return appengine.AppAssertionCredentials(self._SCOPES) with open(credentials_file_path, "r") as credentials_file: credentials_json = json.load(credentials_file) if credentials_json.get("type", None): credentials = GoogleCredentials.from_stream(credentials_file_path) credentials = credentials.create_scoped(self._SCOPES) return credentials return Storage(credentials_file_path).get()
def main(action, zone, instance_name): credentials = GoogleCredentials.from_stream(CREDENTIALS_FILE) compute = discovery.build('compute', 'v1', credentials=credentials) with open(CREDENTIALS_FILE) as jsonfile: data = json.load(jsonfile) project = data["project_id"] if action == "insert": operation = create_instance(compute, project, zone, instance_name) wait_for_operation(compute, project, zone, operation['name']) print(external_ip(compute, project, zone, instance_name)) elif action == "delete": operation = delete_instance(compute, project, zone, instance_name) wait_for_operation(compute, project, zone, operation['name']) else: print("Unknow action") sys.exit(1)
def __init__(self, discovery_file=None, api_version='v1', max_results=100, num_retries=3): if discovery_file is None: if 'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ: raise ValueError("No Google application credentials found. " "A JSON service account key must be either passed as the " "discovery_file argument, or set in the " "GOOGLE_APPLICATION_CREDENTIALS environment variable.") discovery_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] self.credentials = GoogleCredentials.from_stream(discovery_file) self.max_results = max_results self.num_retries = num_retries self.service = discovery.build(self.api_name, api_version, credentials=self.credentials, discoveryServiceUrl=DISCOVERY_URL) super(GoogleAPITransformer, self).__init__()
def get_credentials(credential_file=None, credentials=None): if credential_file: return GoogleCredentials.from_stream(credential_file) if credentials and credentials["type"] == "service_account": return ServiceAccountCredentials_from_dict(credentials) if credentials and credentials["type"] == "authorized_user": return GoogleCredentials( access_token=None, client_id=credentials["client_id"], client_secret=credentials["client_secret"], refresh_token=credentials["refresh_token"], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent="pghoard") return GoogleCredentials.get_application_default()
def google_grpc_channel(self, host, port): """Creates an SSL channel with auth credentials from the environment.""" # In order to make an https call, use an ssl channel with defaults ssl_channel = implementations.ssl_channel_credentials(None, None, None) # Grab application default credentials from the environment creds = GoogleCredentials.from_stream(os.path.join(app_dir,"audio_creds.json")).create_scoped([SPEECH_SCOPE]) # Add a plugin to inject the creds into the header auth_header = ( 'Authorization', 'Bearer ' + creds.get_access_token().access_token) auth_plugin = implementations.metadata_call_credentials( lambda _, cb: cb([auth_header], None), name='google_creds') # compose the two together for both ssl and google auth composite_channel = implementations.composite_channel_credentials( ssl_channel, auth_plugin) return implementations.secure_channel(host, port, composite_channel)
def get_credentials(credential_file=None, credentials=None): if credential_file: return GoogleCredentials.from_stream(credential_file) if credentials and credentials["type"] == "service_account": return _ServiceAccountCredentials( service_account_id=credentials["client_id"], service_account_email=credentials["client_email"], private_key_id=credentials["private_key_id"], private_key_pkcs8_text=credentials["private_key"], scopes=[]) if credentials and credentials["type"] == "authorized_user": return GoogleCredentials( access_token=None, client_id=credentials["client_id"], client_secret=credentials["client_secret"], refresh_token=credentials["refresh_token"], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent="pghoard") return GoogleCredentials.get_application_default()
def get_vision_service(): credentials = GoogleCredentials.from_stream('./application_default_credentials.json') return discovery.build('vision', 'v1', credentials=credentials, discoveryServiceUrl=DISCOVERY_URL)
def get_special_storage_resource(): credentials = GoogleCredentials.from_stream( settings.GOOGLE_APPLICATION_CREDENTIALS).create_scoped(STORAGE_SCOPES) return discovery.build('storage', 'v1', credentials=credentials)
from oauth2client.client import GoogleCredentials import googleapiclient.discovery # Change this values to match your project PROJECT_ID = "tensorflow-class-176106" MODEL_NAME = "earnings" CREDENTIALS_FILE = "credentials.json" # These are the values we want a prediction for inputs_for_prediction = [ {"input": [0.4999, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5]} ] # Connect to the Google Cloud-ML Service credentials = GoogleCredentials.from_stream(CREDENTIALS_FILE) service = googleapiclient.discovery.build('ml', 'v1', credentials=credentials) # Connect to our Prediction Model name = 'projects/{}/models/{}'.format(PROJECT_ID, MODEL_NAME) response = service.projects().predict( name=name, body={'instances': inputs_for_prediction} ).execute() # Report any errors if 'error' in response: raise RuntimeError(response['error']) # Grab the results from the response object results = response['predictions']
def __init__(self): credentials = GoogleCredentials.from_stream(self.SECRET_PATH).create_scoped([ 'https://www.googleapis.com/auth/devstorage.read_write']) http = httplib2.Http() http = credentials.authorize(http) self.service = discovery.build('storage', self.API_VERSION, http=http)
def youtube(cls): """ Prepare YouTube API object """ credentials = GoogleCredentials.from_stream(os.path.expanduser('~/.config/voiceplay/credentials.json')) return build('youtube', 'v3', credentials=credentials)
def __init__(self, bucket): if os.path.isfile(cf_2_gcs.conf.GCS_CREDENTIALS): gcs_credentials = GoogleCredentials.from_stream(cf_2_gcs.conf.GCS_CREDENTIALS) self.gcs_service = discovery.build('storage', 'v1', credentials=gcs_credentials) self.bucket = bucket