1
votes

I am trying to access google drive using the azure function(time-triggered), it creates a token file during runtime when permissions are given to access the drive. It stores that file locally, and the azure function works fine locally.

But when deployed I get an error where my local system path is described in an error that I receive. When I have deployed the function why is it storing my local system path? It should access the path where the Azure function is stored.

Code

        public DriveService GetService()
        {
            //get Credentials from client_secret.json file 
            UserCredential credential;
            string clientSecretString = config[Constant.ClientSecret];
            log.LogInformation("String value is " + clientSecretString);
            byte[] clientSecret = Encoding.UTF8.GetBytes(clientSecretString);
            using (var stream = new MemoryStream(clientSecret)) <----------------- Error Message 
            {

                log.LogInformation("Current path is " + Environment.CurrentDirectory);
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(Environment.CurrentDirectory, false)).Result;
            }

            log.LogInformation("Completed ");
            //create Drive API service.
            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Constant.ApplicationName,
            });
            return service;
        }

Error Message: [Error] at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)at System.Threading.Tasks.Task1.get_Result()at ExportSheetsToExcelPowerBi.GoogleDriveService.GetService() in C:\Users\username\Documents\Project\GoogleDriveService.cs:line

1
Please try to add <None Update="<the path of client_secret.json >"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> and update the code to use relative path to read the file. Then redeploy the application again. - Jim Xu
Do you have any update? - Jim Xu
The path you mentioend in your question is mostly because you are not deploying a relese version. But the path itself it not an issue - Haitham Shaddad

1 Answers

0
votes

For Azure function apps there is no need to read secrets from the token file generated by Google Auth. As answered on one of your previous posts. You can configure your function app to use Google login for authentication purposes when running on Azure. To achieve this you have to generate client id and client secret using the Google sign-in for server-side apps, using this connection you can store the tokens obtained in the token store. Please refer to this document to configure your function app to use Google Login, refer to this document regarding the token store and how to retrieve and refresh the token obtained.