【问题标题】:Accessing Google Drive through Azure Function通过 Azure Function 访问 Google Drive
【发布时间】:2020-09-16 10:51:15
【问题描述】:

任务是下载excel格式的google sheet,并使用Azure时间触发功能将其存储在Azure blob存储中。

用户 google drive 的访问方法 - OAuth Client ID。

我在本地创建了一个 Azure 函数,它按预期工作并执行任务,但是当我部署 azure 函数时出现此错误。

部署时根据堆栈跟踪发生错误的 DriveService 代码

public string[] Scopes = { DriveService.Scope.Drive, DriveService.Scope.DriveReadonly };
public DriveService GetService()
        {
              UserCredential _credential;
 //Error Occurs at line below
            Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow googleAuthFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = _config[Constant.ClientId],
                    ClientSecret = _config[Constant.ClientSecret],
                }
            });

            string FilePath = Path.GetDirectoryName(_driveCredentialsPath);

            _credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                googleAuthFlow.ClientSecrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(FilePath, true)).Result;

            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = _credential,
                ApplicationName = Constant.ApplicationName,
            });
            return service;
        }

我认为有两种情况可能会出错,但我不确定。

  1. 当我在本地运行应用程序时,会出现一个同意屏幕并授予访问驱动器的权限。 当同一功能在 azure 上运行时,谁以及如何授予访问驱动器的权限。

我在 Google OAuth 同意屏幕上提供了我的 Azure 应用 URL,如下所述,以克服这种情况。

  1. 当我在授予访问驱动器权限后在本地运行时,它会创建一个 TOKENRESPONSE-USER 文件 a,其中包含访问令牌、到期日期刷新令牌和范围。

是否有可能在部署函数时无法在 azure 函数上创建 TOKENRESPONSE-USER 文件?

请让我知道为什么我会收到此错误,或者我是否需要更改我的流程中的某些内容。

【问题讨论】:

    标签: azure google-drive-api azure-functions azure-function-app


    【解决方案1】:

    您可以将函数应用配置为在 Azure 上运行时使用 Google 登录进行身份验证。为此,您必须使用 Google 登录为服务器端应用程序生成客户端 ID 和客户端密码,使用此连接您可以将获得的令牌存储在令牌存储中。请参考this文档配置您的函数应用使用Google登录,有关令牌存储以及如何检索和刷新获得的令牌,请参考this文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 2020-10-10
      • 2020-05-23
      • 2013-02-26
      • 1970-01-01
      • 2018-11-10
      • 2021-08-09
      • 2020-02-26
      相关资源
      最近更新 更多