Python googleapiclient.http() Examples

The following are 10 code examples for showing how to use googleapiclient.http(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module googleapiclient , or try the search function .

Example 1
Project: content   Author: demisto   File: GoogleVault.py    License: MIT License 5 votes vote down vote up
def connect():
    creds = get_credentials()
    try:
        service = build('vault', 'v1', http=creds.authorize(Http(disable_ssl_certificate_validation=(not USE_SSL))))
    except Exception as e:
        LOG('There was an error creating the Vault service in the \'connect\' function.')
        err_msg = 'There was an error creating the Vault service - {}'.format(str(e))
        return_error(err_msg)
    return service 
Example 2
Project: content   Author: demisto   File: GoogleVault.py    License: MIT License 5 votes vote down vote up
def download_storage_object(object_ID, bucket_name):
    service = connect_to_storage()
    req = service.objects().get_media(bucket=bucket_name, object=object_ID)  # pylint: disable=no-member
    out_file = io.BytesIO()
    downloader = googleapiclient.http.MediaIoBaseDownload(out_file, req)
    done = False
    while not done:
        done = downloader.next_chunk()[1]
    return out_file 
Example 3
Project: content   Author: demisto   File: GoogleVault.py    License: MIT License 5 votes vote down vote up
def connect_to_storage():
    try:
        creds = get_storage_credentials()
        ptth = authorized_http(creds)
        ptth.disable_ssl_certificate_validation = (not USE_SSL)
        service = build('storage', 'v1', http=ptth)
    except Exception as e:
        LOG('There was an error creating the Storage service in the \'connect_to_storage\' function.')
        err_msg = 'There was an error creating the Storage service - {}'.format(str(e))
        return_error(err_msg)
    return service 
Example 4
Project: python-docs-samples   Author: GoogleCloudPlatform   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def upload_object(self, bucket, file_object):
        body = {
            'name': 'storage-api-client-sample-file.txt',
        }
        req = storage.objects().insert(
            bucket=bucket, body=body,
            media_body=googleapiclient.http.MediaIoBaseUpload(
                file_object, 'application/octet-stream'))
        resp = req.execute()
        return resp 
Example 5
Project: python-docs-samples   Author: GoogleCloudPlatform   File: crud_object.py    License: Apache License 2.0 5 votes vote down vote up
def create_service():
    # Construct the service object for interacting with the Cloud Storage API -
    # the 'storage' service, at version 'v1'.
    # You can browse other available api services and versions here:
    #     http://g.co/dv/api-client-library/python/apis/
    return googleapiclient.discovery.build('storage', 'v1') 
Example 6
Project: python-docs-samples   Author: GoogleCloudPlatform   File: crud_object.py    License: Apache License 2.0 5 votes vote down vote up
def upload_object(bucket, filename, readers, owners):
    service = create_service()

    # This is the request body as specified:
    # http://g.co/cloud/storage/docs/json_api/v1/objects/insert#request
    body = {
        'name': filename,
    }

    # If specified, create the access control objects and add them to the
    # request body
    if readers or owners:
        body['acl'] = []

    for r in readers:
        body['acl'].append({
            'entity': 'user-%s' % r,
            'role': 'READER',
            'email': r
        })
    for o in owners:
        body['acl'].append({
            'entity': 'user-%s' % o,
            'role': 'OWNER',
            'email': o
        })

    # Now insert them into the specified bucket as a media insertion.
    # http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#insert
    with open(filename, 'rb') as f:
        req = service.objects().insert(
            bucket=bucket, body=body,
            # You can also just set media_body=filename, but for the sake of
            # demonstration, pass in the more generic file handle, which could
            # very well be a StringIO or similar.
            media_body=googleapiclient.http.MediaIoBaseUpload(
                f, 'application/octet-stream'))
        resp = req.execute()

    return resp 
Example 7
Project: python-docs-samples   Author: GoogleCloudPlatform   File: crud_object.py    License: Apache License 2.0 5 votes vote down vote up
def get_object(bucket, filename, out_file):
    service = create_service()

    # Use get_media instead of get to get the actual contents of the object.
    # http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#get_media
    req = service.objects().get_media(bucket=bucket, object=filename)

    downloader = googleapiclient.http.MediaIoBaseDownload(out_file, req)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))

    return out_file 
Example 8
Project: addon-hassiogooglebackup   Author: samccauley   File: gbcommon.py    License: MIT License 5 votes vote down vote up
def getDriveService(user_agent):

    with open(TOKEN) as f:
        creds = json.load(f)

    credentials = GoogleCredentials(None,creds["client_id"],creds["client_secret"],
                                          creds["refresh_token"],None,"https://accounts.google.com/o/oauth2/token",user_agent)
    http = credentials.authorize(Http())
    credentials.refresh(http)
    drive_service = build('drive', 'v3', http)

    return drive_service 
Example 9
Project: addon-hassiogooglebackup   Author: samccauley   File: gbcommon.py    License: MIT License 5 votes vote down vote up
def backupFile(fileName, backupDirID, drive_service, MIMETYPE, TITLE, DESCRIPTION):

    logging.info("Backing up " + fileName + " to " + backupDirID)

    logging.debug("drive_service = " + str(drive_service))
    logging.debug("MIMETYPE = " + MIMETYPE)
    logging.debug("TITLE = " + TITLE)
    logging.debug("DESCRIPTION = " + DESCRIPTION)

    shortFileName = ntpath.basename(fileName)

    media_body = googleapiclient.http.MediaFileUpload(
        fileName,
        mimetype=MIMETYPE,
        resumable=True
    )

    logging.debug("media_body: " + str(media_body))

    body = {
        'name': shortFileName,
        'title': TITLE,
        'description': DESCRIPTION,
        'parents': [backupDirID]
    }

    new_file = drive_service.files().create(
    body=body, media_body=media_body).execute()
    logging.debug(pformat(new_file)) 
Example 10
Project: google-drive-recursive-ownership   Author: davidstrauss   File: transfer.py    License: MIT License 5 votes vote down vote up
def get_drive_service():
    OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive'
    CLIENT_SECRETS = 'client_secrets.json'
    flow = oauth2client.client.flow_from_clientsecrets(CLIENT_SECRETS, OAUTH2_SCOPE)
    flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
    authorize_url = flow.step1_get_authorize_url()
    print('Use this link for authorization: {}'.format(authorize_url))
    code = six.moves.input('Verification code: ').strip()
    credentials = flow.step2_exchange(code)
    http = httplib2.Http()
    credentials.authorize(http)
    drive_service = googleapiclient.discovery.build('drive', 'v2', http=http)
    return drive_service