0

I am trying to build GC function to read a CSV file from Google drive and then based on that information send an email to myself. Once this function works, then I want to use GC scheduler to runt this code every 6 hours.

So far I have managed to build a simple Python code that works locally on my computer, uses oAuth2.0 to access the drive and send email. I am using Pydrive, settings.yaml & client_secrets.json to authenticate and gain access to the drive using this post.Automating pydrive verification process

However none of this works on the GC Function. I managed to successfully deploy the code, however when I run test, it crashes. When I check the logs, I see that the code is able to retrieve the URL that it needs me to allow access for the 1st time, and then it crashes. In the logs it also says "Your browser has been opened to visit:", but nothing opens on my screen but a "unexpected error message prompt". It is clear that the function cannot create credentials.json for the next use too. I wonder if this method will ever work on GC functions. I have found "https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail" this post that uses Javascript to gain access, but nothing in Python so far. Has anybody done this in Python? I am new to Python, OAuth2.0 and GCP ( probably you figured it all by my questions)

Please find below the part of my code that does the authentication + main function that calls this authentication and reads the file: I also have the secrets.json and settings.yaml file, in the same directory which I believe they allow the code to work on my laptop. Please note: I didnt include couple of functions in the middle which are responsible to get the price and send emails. Please let me know if you need those too? Do you need the settings.yaml file too?

from  pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials
import pandas as pd

def cred_test_authentication():
    gauth = GoogleAuth()
    # Try to load saved client credentials
    gauth.LoadCredentialsFile("mycreds.txt")
   if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
   elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
   else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
    gauth.SaveCredentialsFile("mycreds.txt")

   drive = GoogleDrive(gauth)

   downloaded = drive.CreateFile({'id':"FileID"})  
   downloaded.GetContentFile('Myalert.csv')
   data_1 = pd.read_csv('Myalert.csv') 
   return data_1


def main(data,context):


    df=cred_test_authentication()
    chg=False
    body = 'Changes:\n'

    for row in df.itertuples():
        value_1= quote_grab(row.symbol)
        if value_1 > row.target_price:
            body = body + 'Price for %s went up to %s (configured_price = %s)\n' % (row.symbol, value_1, row.target_price )
            chg = True
       if value_1 < row.target_price:
            body = body + 'Price for %s went down to %s (configured_price = %s)\n' % (row.symbol,value_1, row.target_price )
            chg = True
     if chg:
         print ('sending email...')
         send_email(' Price Changes Alert',body)


   if __name__ == "__main__":
    main()
Maryam Ar
  • 1
  • 2

0 Answers0