def test_signed_jwt_assertion_credentials(self):
   private_key = datafile('privatekey.p12')
   credentials = SignedJwtAssertionCredentials(
       '*****@*****.**',
       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'])
Example #2
0
def rename_village_sheet(village_docid, new_title):
    conf = local_config['spreadsheet']
    credentials = SignedJwtAssertionCredentials(conf['client_email'],
                                                conf['private_key'], scope)

    http = httplib2.Http()
    http = credentials.authorize(http)
    drive_service = build('drive', 'v2', http=http)

    file = drive_service.files().get(fileId=village_docid).execute()
    file['title'] = new_title
    updated_file = drive_service.files().update(fileId=village_docid,
                                                body=file).execute()
    return "success"
def login_open_sheet(oauth_key_file, spreadsheet):
	"""Connect to Google Docs spreadsheet and return the first worksheet."""
	try:
		json_key = json.load(open(oauth_key_file))
		credentials = SignedJwtAssertionCredentials(json_key['client_email'], 
													json_key['private_key'], 
													['https://spreadsheets.google.com/feeds'])
		gc = gspread.authorize(credentials)
		worksheet = gc.open(spreadsheet).sheet1
		return worksheet
	except Exception as ex:
		print 'Unable to login and get spreadsheet.  Check OAuth credentials, spreadsheet name, and make sure spreadsheet is shared to the client_email address in the OAuth .json file!'
		print 'Google sheet login failed with error:', ex
		sys.exit(1)
Example #4
0
    def _get_bigquery_service(self):
        scope = [
            "https://www.googleapis.com/auth/bigquery",
        ]

        key = json.loads(b64decode(self.configuration['jsonKeyFile']))

        credentials = SignedJwtAssertionCredentials(key['client_email'],
                                                    key['private_key'],
                                                    scope=scope)
        http = httplib2.Http(timeout=settings.BIGQUERY_HTTP_TIMEOUT)
        http = credentials.authorize(http)

        return build("bigquery", "v2", http=http)
Example #5
0
    def test_JWTServiceAccount(self):
        f = file(PRIVATE_KEY, 'rb')
        key = f.read()
        f.close()

        credentials = SignedJwtAssertionCredentials(service_account_name=SERVICE_ACCOUNT_EMAIL,
                                                    private_key=key,
                                                    scope=" ".join(SCOPES),
                                                    prn=USERNAME)

        imap = GMail_IMAP()
        imap.login_oauth2(USERNAME, credentials=credentials)

        imap.select("INBOX")
Example #6
0
    def __init__(self, parsed_url):
        duplicity.backend.Backend.__init__(self, parsed_url)
        try:
            global pydrive
            import httplib2
            from apiclient.discovery import build
            from oauth2client.client import SignedJwtAssertionCredentials
            from pydrive.auth import GoogleAuth
            from pydrive.drive import GoogleDrive
            from pydrive.files import FileNotUploadedError
        except ImportError:
            raise BackendException('PyDrive backend requires PyDrive installation'
                                   'Please read the manpage to fix.')

        if 'GOOGLE_DRIVE_ACCOUNT_KEY' in os.environ:
            account_key = os.environ['GOOGLE_DRIVE_ACCOUNT_KEY']
            credentials = SignedJwtAssertionCredentials(parsed_url.username + '@' + parsed_url.hostname, account_key, scope='https://www.googleapis.com/auth/drive')
            credentials.authorize(httplib2.Http())
            gauth = GoogleAuth()
            gauth.credentials = credentials
        elif 'GOOGLE_DRIVE_SETTINGS' in os.environ:
            gauth = GoogleAuth(settings_file=os.environ['GOOGLE_DRIVE_SETTINGS'])
            gauth.CommandLineAuth()
        else:
            raise BackendException('GOOGLE_DRIVE_ACCOUNT_KEY or GOOGLE_DRIVE_SETTINGS environment variable not set. Please read the manpage to fix.')
        self.drive = GoogleDrive(gauth)

        # Dirty way to find root folder id
        file_list = self.drive.ListFile({'q': "'Root' in parents and trashed=false"}).GetList()
        if file_list:
            parent_folder_id = file_list[0]['parents'][0]['id']
        else:
            file_in_root = self.drive.CreateFile({'title': 'i_am_in_root'})
            file_in_root.Upload()
            parent_folder_id = file_in_root['parents'][0]['id']

        # Fetch destination folder entry and create hierarchy if required.
        folder_names = string.split(parsed_url.path, '/')
        for folder_name in folder_names:
            if not folder_name:
                continue
            file_list = self.drive.ListFile({'q': "'" + parent_folder_id + "' in parents and trashed=false"}).GetList()
            folder = next((item for item in file_list if item['title'] == folder_name and item['mimeType'] == 'application/vnd.google-apps.folder'), None)
            if folder is None:
                folder = self.drive.CreateFile({'title': folder_name, 'mimeType': "application/vnd.google-apps.folder", 'parents': [{'id': parent_folder_id}]})
                folder.Upload()
            parent_folder_id = folder['id']
        self.folder = parent_folder_id
        self.id_cache = {}
Example #7
0
def publish():
    form = Form()
    if form.validate_on_submit():
        try:
            # TODO: add this to config instead
            json_filepath = os.path.join('C:\\', 'Users', 'michhar', 'Documents', 'MLADS', 'data', 'MessyDoc-8f814e3f2a78.json')
            json_key = json.load(open(json_filepath))

            credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), config.SCOPE)

            gc = gspread.authorize(credentials)

            # TODO: create a worksheet if not there, also put this in config
            wksheet = gc.open("SSF_Crop_Master_2012_Master_crop_master").worksheet('latest')

            # make a client connection
            client = document_client.DocumentClient(config.DOCUMENTDB_HOST, {'masterKey': config.DOCUMENTDB_KEY})

            # Read databases and get our working database
            db = next((data for data in client.ReadDatabases() if data['id'] == config.DOCDB_DATABASE))

            # Read collections and get the "user collection"
            coll_master = next((coll for coll in client.ReadCollections(db['_self']) if coll['id'] == config.DOCDB_COLLECTION_MASTER))


            master_doc = next((doc for doc in client.ReadDocuments(coll_master['_self']) if doc['id'] == config.DOCDB_MASTER_DOC))
            master_data_df = read_json(master_doc['data'])
            headers = read_json(master_doc['data_headers'])
            master_data_df.columns = headers

            # update all cells in master google doc with data in master doc from db
            # this takes a minute or two (maybe put into a separate view function)
            update_worksheet(wksheet, master_data_df)

            return render_template('results.html',
                        masterlink = 'https://docs.google.com/spreadsheets/d/1MKcDtjI5E-iNv9tU2KcA5yJWWgaSTh5j2IjPYOp9lic/pubhtml',
                        title = 'Results',
                        year = datetime.now().year,
                        message = 'Success! Your data has been stored and the master sheet updated here ')

        except gspread.SpreadsheetNotFound, e:

            return render_template('error_page.html',
                                   title = 'Something went wrong!',
                                   year = datetime.now().year,
                                   message = '''The spreadsheet was not found.
                                   Please ensure you have enabled Google Drive API and
                                   created a new set of credentials.''',
                                   link = 'http://gspread.readthedocs.org/en/latest/oauth2.html')
Example #8
0
def insert_collection(Category,FileLocation,VideoName,URLName,URL,IsTimeMatch,IsNameMatch):
	reload(sys)  
	sys.setdefaultencoding('Cp1252')
	json_key = json.load(open('NitishaProject-82d3623f7c3e.json'))
	scope = ['https://spreadsheets.google.com/feeds']

	credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)

	gc = gspread.authorize(credentials)

	sheet = gc.open_by_key('1JlAE1J8zmG6-C_pasKg1C8KjhJ3wWwXO9a2CqM254B0')
	worksheet = sheet.get_worksheet(0)
	#worksheet.resize(rows=1, cols=5)
	rowtoadd=[Category,FileLocation,VideoName,URLName,URL,IsTimeMatch,IsNameMatch]
	worksheet.append_row(rowtoadd)
Example #9
0
 def read_spsht(id_or_url, sname, sp_credential_file, sp_scope, *args, **kwargs):
     try:
         from oauth2client.service_account import ServiceAccountCredentials
         gc = gspread.authorize(ServiceAccountCredentials.from_json_keyfile_name(sp_credential_file, sp_scope))
     except:
         from oauth2client.client import SignedJwtAssertionCredentials
         json_key = json.loads(os.read(os.open(sp_credential_file, os.O_RDONLY),9999999))
         gc = gspread.authorize(SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), sp_scope))
     try:
         sp = gc.open_by_key(id_or_url)
     except:
         sp = gc.open_by_url(id_or_url)
     ws = sp.worksheet(sname)
     values = ws.get_all_values()
     return pd.DataFrame(values, *args, **kwargs)
Example #10
0
def get_google_auth():
    import json
    import gspread
    from oauth2client.client import SignedJwtAssertionCredentials

    from credentials import GOOGLE_OAUTH_JSON_FILE

    json_key = json.load(open(GOOGLE_OAUTH_JSON_FILE))
    scope = ["https://spreadsheets.google.com/feeds"]

    # authenticate
    credentials = SignedJwtAssertionCredentials(
        json_key["client_email"], json_key["private_key"].encode("utf-8"),
        scope)
    return gspread.authorize(credentials)
Example #11
0
def get_credentials(username):
    """Gets valid user credentials from a JSON service account key file.

  Args:
    username: The email address of the user to impersonate.
  Returns:
    Credentials, the obtained credential.
  """
    json_file = json.load(open(args.json))
    credentials = SignedJwtAssertionCredentials(json_file['client_email'],
                                                json_file['private_key'],
                                                SCOPES,
                                                sub=username)

    return credentials
Example #12
0
    def authorize_pki(self,
                      client_id,
                      pki,
                      scope=YOUTUBE_ADMIN_SCOPE,
                      as_user=None):
        from oauth2client.client import SignedJwtAssertionCredentials
        self._credentials = SignedJwtAssertionCredentials(client_id,
                                                          pki,
                                                          scope,
                                                          sub=as_user)

        h = self._credentials.authorize(httplib2.Http())
        self.youtube_service = build(self.YOUTUBE_API_SERVICE_NAME,
                                     self.YOUTUBE_API_VERSION,
                                     http=h)
Example #13
0
def get_sheet(s):
    # use creds to create a client to interact with the Google Drive API
    scope = ['https://spreadsheets.google.com/feeds']
    try:
        client_secret = json.loads(
            os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
    except KeyError:
        client_secret = json.load(open('client_secret.json'))
    client_email = client_secret['client_email']
    private_key = client_secret['private_key']

    creds = SignedJwtAssertionCredentials(client_email, private_key, scope)
    gc = gspread.authorize(creds)
    sheet = gc.open(s)
    return sheet
Example #14
0
def get_conn():
    with open(FULL_PATH_TO_P12) as f:
        private_key = f.read()

    OAUTH_SCOPE = [
        'https://www.googleapis.com/auth/admin.directory.group.member',
        'https://www.googleapis.com/auth/admin.directory.group'
    ]

    credentials = SignedJwtAssertionCredentials(SERVICE_EMAIL,
                                                private_key,
                                                OAUTH_SCOPE,
                                                sub=ACCOUNT_EMAIL)
    http = httplib2.Http()
    return credentials.authorize(http)
Example #15
0
def auth_and_fetch():
    try:
        json_key = json.load(open(path_to_key))
        scope = ['https://spreadsheets.google.com/feeds']
        #auth
        credentials = SignedJwtAssertionCredentials(json_key['client_email'],
                                                    json_key['private_key'],
                                                    scope)
        gc = gspread.authorize(credentials)
        #get the doc
        document = gc.open_by_url(path_to_google_doc)
        sheet = document.sheet1
        return sheet
    except Exception, e:
        raise Exception('unable to authenticate and fetch the doc : ' + str(e))
Example #16
0
    def __auth():
        # TODO Validate credentials
        private_key = open(settings.GOOGLE_PREDICTION_PRIVATE_KEY).read()
        http = httplib2.Http()

        credentials = SignedJwtAssertionCredentials(
            settings.GOOGLE_PREDICTION_PROJECT_EMAIL, private_key, [
                'https://www.googleapis.com/auth/devstorage.full_control',
                'https://www.googleapis.com/auth/devstorage.read_only',
                'https://www.googleapis.com/auth/devstorage.read_write',
                'https://www.googleapis.com/auth/prediction',
            ])

        credentials.authorize(http)
        APIManager.__api = discovery.build('prediction', 'v1.6', http=http)
def fetch_news():
    try:
        vect = pickle.load(open(r'/Users/alexcombs/Downloads/news_vect_pickle.p', 'rb'))
        model = pickle.load(open(r'/Users/alexcombs/Downloads/news_model_pickle.p', 'rb'))

        json_key = json.load(open(r'/Users/alexcombs/Downloads/API Project-5d8d50bccf0b.json'))
        scope = ['https://spreadsheets.google.com/feeds']
        credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
        gc = gspread.authorize(credentials)


        ws = gc.open("NewStories")
        sh = ws.sheet1
        zd = list(zip(sh.col_values(2),sh.col_values(3), sh.col_values(4)))
        zf = pd.DataFrame(zd, columns=['title','urls','html'])
        zf.replace('', pd.np.nan, inplace=True)
        zf.dropna(inplace=True)

        def get_text(x):
            soup = BeautifulSoup(x, 'lxml')
            text = soup.get_text()
            return text

        zf.loc[:,'text'] = zf['html'].map(get_text)

        tv = vect.transform(zf['text'])
        res = model.predict(tv)

        rf = pd.DataFrame(res, columns=['wanted'])
        rez = pd.merge(rf, zf, left_index=True, right_index=True)

        news_str = ''
        for t, u in zip(rez[rez['wanted']=='y']['title'], rez[rez['wanted']=='y']['urls']):
            news_str = news_str + t + '\n' + u + '\n'

        payload = {"value1" : news_str}
        r = requests.post('https://maker.ifttt.com/trigger/news_event/with/key/banZCjMLOotibc4WguJx0B', data=payload)

        # clean up worksheet
        lenv = len(sh.col_values(1))
        cell_list = sh.range('A1:F' + str(lenv))
        for cell in cell_list:
            cell.value = ""
        sh.update_cells(cell_list)
        print(r.text)
        
    except:
        print('Failed')
Example #18
0
def main():
    # Load the key in PKCS 12 format that you downloaded from the Google API
    # Console when you created your Service account.
    with file('YOUR-KEY-HERE-privatekey.p12', 'rb') as key_file:
        key = key_file.read()

    # Create an httplib2.Http object to handle our HTTP requests and to
    # authorize them correctly.
    #
    # Note that the first parameter, service_account_name, is the Email address
    # created for the Service account. It must be the email address associated
    # with the key that was created.
    credentials = SignedJwtAssertionCredentials(
        'YOUR-SERVICE-ACCOUNT-EMAIL',
        key,
        scope='https://www.googleapis.com/auth/mapsengine')
    http = httplib2.Http()
    http = credentials.authorize(http)

    # construct the JSON packet
    json_data = {
        'name': 'perth',
        'files': [{
            'filename': 'perth1.jpg'
        }, {
            'filename': 'perth2.jpg'
        }],
        'acquisitionTime': '1979-01-01',
        'attribution': 'Copyright Spatial Data API Testing',
        'sharedAccessList': 'Map Editors'
    }

    # Read the first page of features in a Table.
    headers = {'Content-type': 'application/json'}
    uri = 'https://www.googleapis.com/mapsengine/create_tt/rasters/upload?projectId=10258059232491603613'

    response, content = http.request(uri,
                                     "POST",
                                     headers=headers,
                                     body=json.dumps(json_data))
    resource = json.loads(content.decode('utf-8'))
    if response.status == 200:
        print "Asset created with id: ", resource['id']
        for i in json_data['files']:
            filename = i['filename']
            uploadImage(resource['id'], filename, http)
    else:
        print('Error: ', resource['error'])
Example #19
0
def list_instances(client_cert_file, client_email, project, zone):
    with open(client_cert_file) as f:
        private_key = f.read()
    credentials = SignedJwtAssertionCredentials(
        client_email, private_key,
        'https://www.googleapis.com/auth/compute.readonly')
    http = Http()
    credentials.authorize(http)
    try:
        compute = build('compute', 'v1', http=http)
        resp_json = compute.instances().list(project=project,
                                             zone=zone).execute()
    except HttpError:
        raise GoogleClientError(
            'Failed to make "list instances" Google cloud API request')
    return resp_json
Example #20
0
    def run(self, dispatcher, tracker, domain):

        json_key = json.load(
            open("creds.json"))  # json credentials you downloaded earlier
        scope = [
            'https://spreadsheets.google.com/feeds',
            'https://www.googleapis.com/auth/drive'
        ]
        credentials = SignedJwtAssertionCredentials(
            json_key['client_email'], json_key['private_key'].encode(),
            scope)  # get email and key from creds
        file = gspread.authorize(credentials)
        client = "Client1"  #get the name of the client
        sheet = file.open("Comptes Clients").worksheet(client)
        balance = sheet.cell(3, 2).value
        return [SlotSet("SOLDE"), balance]
Example #21
0
def _gce_conn(cred):
    service_account_name = cred['service_account_name']
    key = cred['key']

    # convert pkcs12 to rsa
    out, err, ret_code = helper.call(
        "openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa",
        input=binascii.a2b_base64(key),
        shell=True)
    key = out.strip()

    signed_jwt_assert_cred = SignedJwtAssertionCredentials(
        service_account_name, key, ['https://www.googleapis.com/auth/compute'])
    http = httplib2.Http()
    http = signed_jwt_assert_cred.authorize(http)
    return build('compute', 'v1', http=http), http
Example #22
0
def get_ms_monitor_reference_data(
        notebook_name="20160203 ms-monitor reference data",
        token='/project/projectdirs/metatlas/projects/google_sheets_auth/ipython to sheets demo-9140f8697062.json',
        sheet_name='QC and ISTD'):
    json_key = json.load(open(token))
    scope = ['https://spreadsheets.google.com/feeds']

    credentials = SignedJwtAssertionCredentials(
        json_key['client_email'], json_key['private_key'].encode(), scope)

    gc = gspread.authorize(credentials)

    wks = gc.open(notebook_name)
    istd_qc_data = wks.worksheet(sheet_name).get_all_values()
    #     blank_data = wks.worksheet('BLANK').get_all_values()
    return istd_qc_data  #, blank_data
Example #23
0
def googleSpreadSheetToCSVFiles(credentialsJsonFile, spreadSheetKey,
                                outputDirectory):
    json_key = json.load(open(credentialsJsonFile))
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = SignedJwtAssertionCredentials(json_key['client_email'],
                                                json_key['private_key'], scope)
    connection = gspread.authorize(credentials)
    spreadsheet = connection.open_by_key(spreadSheetKey)
    generated_files = []
    for sheet in spreadsheet.worksheets():
        output_filename = os.path.join(outputDirectory, sheet.title + '.csv')
        with open(output_filename, 'wb') as f:
            writer = unicodecsv.writer(f, encoding='utf-8')
            writer.writerows(sheet.get_all_values())
        generated_files.append(output_filename)
    return generated_files
Example #24
0
    def connect(self):
        ''' Connect to Google '''

        # Login with a Google account
        scope = ['https://spreadsheets.google.com/feeds']
        credentials = SignedJwtAssertionCredentials(self.email, self.password,
                                                    scope)
        gc = gspread.authorize(credentials)

        # Open the spreadsheet and worksheet
        spreadsheet = gc.open(self.sheetname)
        self.wks = spreadsheet.worksheet(self.tabname)

        # Mark the cache invalid so that it will get refreshed on the
        # next check
        self.cache_valid = False
Example #25
0
 def _create_connection(self):
     platform = node.__node__['platform']
     http = httplib2.Http()
     try:
         email = platform.get_access_data('service_account_name')
         pk = base64.b64decode(platform.get_access_data('key'))
     except platform.PlatformError:
         raise NoCredentialsError(sys.exc_info()[1])
     try:
         cred = SignedJwtAssertionCredentials(email, pk, scope=self.scope)
         conn = build(self.service_name,
                      self.api_version,
                      http=cred.authorize(http))
     except:
         raise InvalidCredentialsError(sys.exc_info()[1])
     return BadStatusLineHandler(conn)
Example #26
0
def run():
	json_key = json.load(open('varsity-athletics-bdce819be77f.json'))
	scope = ['https://spreadsheets.google.com/feeds']

	credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)

	gc = gspread.authorize(credentials)

	wb = gc.open("Varsity Athletics 2016")


	wks = wb.get_worksheet(1)
	stuff = wks.get_all_values()
	print "Got it!"
	print
	print stuff
Example #27
0
def get_credentials():
    global __credentials

    if __credentials:
        return __credentials
    else:
        # Loads the key file's private data.
        with open(KEY_FILEPATH) as key_file:
            key_data = json.load(key_file)

        # Constructs a credentials objects from the key data and OAuth2 scope.
        __credentials = SignedJwtAssertionCredentials(key_data['client_email'],
                                                      key_data['private_key'],
                                                      SCOPE)

        return __credentials
def createDriveService(config):
  """Builds and returns a Drive service object authorized with the given config object.
  Returns:
    Drive service object.
  """

  f = file(config['private_key12_path'], 'rb')
  key = f.read()
  f.close()
  # config.service_account Email of the Service Account.
  credentials = SignedJwtAssertionCredentials(config['service_account'], key,
      scope='https://www.googleapis.com/auth/drive')
  http = httplib2.Http()
  http = credentials.authorize(http)

  return build('drive', 'v2', http=http)
Example #29
0
def add_cells():
    print 'connecting ...'
    json_key = json.load(open('bw-key.json'))
    scope = ['https://spreadsheets.google.com/feeds']
    credentials = SignedJwtAssertionCredentials(
        json_key['client_email'], json_key['private_key'].encode(), scope)
    gc = gspread.authorize(credentials)
    wks = gc.open("Beer Warmer").sheet1
    d = datetime.today().date()
    dt = d.isoformat()
    t = datetime.now().time()
    tt = t.isoformat()
    print(dt)
    print(tt)
    print(read_temp())
    wks.append_row([dt, tt, read_temp()])
Example #30
0
def load_listings(
        oauthfile='/media/roberto/Main Storage/Documents/Oauth/google/API Project-c86cee112445.json',
        scope=['https://spreadsheets.google.com/feeds']):
    ''' load google spreadsheet with data '''

    json_key = json.load(open(oauthfile))
    credentials = SignedJwtAssertionCredentials(json_key['client_email'],
                                                json_key['private_key'], scope)

    # Authorize
    gspreadclient = gspread.authorize(credentials)

    # Load spreadsheet
    spreadsheet = gspreadclient.open('current amazon listings')

    return (gspreadclient, spreadsheet)