1

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

bippan
  • 239
  • 1
  • 2
  • 11

1 Answers1

0

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.

ChaitanyaN-MSFT
  • 466
  • 2
  • 5
  • This method does not work because there is no place on Azure to specify Google Drive scopes. without scopes at the time of authentication, when a DriveService is created using the automatically generated access token, it does not have enough permission to do any operation. – Soli Oct 28 '20 at 22:27