Exemplo n.º 1
0
def _google_sheet_credentials_from_service_account_credentials(
        private_key_id: str, private_key: str, client_email: str,
        client_id: str):
    '''Returns the credentials for a service account

    The credentials have the scope set to ['https://spreadsheets.google.com/feeds']

    https://gspread.readthedocs.io/en/latest/oauth2.html

    '''
    import oauth2client
    from oauth2client.service_account import ServiceAccountCredentials
    from oauth2client import crypt

    # adapted from ServiceAccountCredentials._from_parsed_json_keyfile()
    service_account_email = client_email
    private_key_pkcs8_pem = private_key
    private_key_id = private_key_id
    client_id = client_id
    token_uri = oauth2client.GOOGLE_TOKEN_URI
    revoke_uri = oauth2client.GOOGLE_REVOKE_URI

    signer = crypt.Signer.from_string(private_key_pkcs8_pem)
    credentials = ServiceAccountCredentials(service_account_email,
                                            signer,
                                            scopes=SCOPES,
                                            private_key_id=private_key_id,
                                            client_id=client_id,
                                            token_uri=token_uri,
                                            revoke_uri=revoke_uri)
    credentials._private_key_pkcs8_pem = private_key_pkcs8_pem

    return credentials
def _google_analytics_credentials_from_service_account_credentials(
    private_key_id: str,
    private_key: str,
    client_email: str,
    client_id: str
):
    '''Returns the credentials for a service account
    '''
    import oauth2client
    from oauth2client.service_account import ServiceAccountCredentials
    from oauth2client import crypt

    # adapted from ServiceAccountCredentials._from_parsed_json_keyfile()
    service_account_email = client_email
    private_key_pkcs8_pem = private_key
    private_key_id = private_key_id
    client_id = client_id
    token_uri = oauth2client.GOOGLE_TOKEN_URI
    revoke_uri = oauth2client.GOOGLE_REVOKE_URI

    signer = crypt.Signer.from_string(private_key_pkcs8_pem)
    credentials = ServiceAccountCredentials(service_account_email, signer, scopes=SCOPES,
                                            private_key_id=private_key_id,
                                            client_id=client_id, token_uri=token_uri,
                                            revoke_uri=revoke_uri)
    credentials._private_key_pkcs8_pem = private_key_pkcs8_pem

    return credentials
Exemplo n.º 3
0
 def __init__(self, client_email, private_key, private_key_id, client_id):
     service_account_email = client_email
     private_key_pkcs8_pem = private_key
     private_key_id = private_key_id
     signer = crypt.Signer.from_string(private_key_pkcs8_pem)
     credentials = ServiceAccountCredentials(service_account_email,
                                             signer,
                                             scopes=SCOPES,
                                             private_key_id=private_key_id,
                                             client_id=client_id)
     credentials._private_key_pkcs8_pem = private_key_pkcs8_pem
     # Build the service object.
     self.service = build('calendar', 'v3', credentials=credentials)
Exemplo n.º 4
0
 def _make_credentials(self):
     private_key = datafile('privatekey.' + self.format_)
     signer = crypt.Signer.from_string(private_key)
     credentials = ServiceAccountCredentials(
         '*****@*****.**', signer,
         scopes='read+write',
         sub='*****@*****.**')
     if self.format_ == 'pem':
         credentials._private_key_pkcs8_pem = private_key
     elif self.format_ == 'p12':
         credentials._private_key_pkcs12 = private_key
         credentials._private_key_password = _PASSWORD_DEFAULT
     else:  # pragma: NO COVER
         raise ValueError('Unexpected format.')
     return credentials
Exemplo n.º 5
0
def get_creds():
    scopes = ''
    service_account_email = os.environ['goog_client_email']
    private_key_pkcs8_pem = os.environ['goog_private_key'].replace(r"\n", "\n")
    private_key_id = os.environ['goog_private_key_id']
    client_id = os.environ['goog_client_id']
    token_uri = os.environ['goog_token_uri']
    revoke_uri = GOOGLE_REVOKE_URI
    signer = crypt.Signer.from_string(private_key_pkcs8_pem)
    credentials = ServiceAccountCredentials(service_account_email,
                                            signer,
                                            scopes=scopes,
                                            private_key_id=private_key_id,
                                            client_id=client_id,
                                            token_uri=token_uri,
                                            revoke_uri=revoke_uri)
    credentials._private_key_pkcs8_pem = private_key_pkcs8_pem
    return credentials
Exemplo n.º 6
0
 def credentials_from_env(self, scopes):
     keyfile_dict = {}
     keyfile_dict["token_uri"] = get_env('SC_TOKEN_URI')
     keyfile_dict["auth_uri"] = get_env('SC_AUTH_URI')
     # TODO: clean this part
     tmp = get_env('SC_PRIVATE_KEY').replace("\n", "\\n")
     buff = '{"a": "' + tmp + '"}'
     buff = json.loads(buff)
     keyfile_dict["private_key"] = buff['a']
     #######################
     signer = crypt.Signer.from_string(keyfile_dict["private_key"])
     credential = ServiceAccountCredentials(
         get_env('SC_CLIENT_EMAIL'),
         signer,
         scopes=scopes,
         private_key_id=get_env('SC_PRIVATE_KEY_ID'),
         client_id=get_env('SC_CLIENT_ID'),
         token_uri=keyfile_dict.get('token_uri', GOOGLE_TOKEN_URI),
         revoke_uri=keyfile_dict.get('revoke_uri', GOOGLE_REVOKE_URI),
     )
     credential._private_key_pkcs8_pem = keyfile_dict['private_key']
     return credential
Exemplo n.º 7
0
def get_gspread_credentials():
    ''' Returns a Google Spreadsheets credentials object.
    '''
    try:
        with open('gspread_settings.json', 'r') as f:
            settings = json.load(f)

        service_account_email = settings['client_email']
        private_key_pkcs8_pem = settings['private_key']
        private_key_id = settings['private_key_id']
        client_id = settings['client_id']
    except:
        service_account_email = os.environ['GSPREAD_CLIENT_EMAIL']
        private_key_pkcs8_pem = os.environ['GSPREAD_PRIVATE_KEY']
        private_key_id = os.environ['GSPREAD_PRIVATE_KEY_ID']
        client_id = os.environ['GSPREAD_CLIENT_ID']

    signer = crypt.Signer.from_string(private_key_pkcs8_pem)
    credentials = ServiceAccountCredentials(service_account_email, signer,
                                            scopes='https://spreadsheets.google.com/feeds', 
                                            private_key_id=private_key_id, client_id=client_id)
    credentials._private_key_pkcs8_pem = private_key_pkcs8_pem

    return credentials