【问题标题】:How to read a accessToken file that's located in a App_Data folder?如何读取位于 App_Data 文件夹中的 accessToken 文件?
【发布时间】:2020-04-01 02:05:26
【问题描述】:

我有这个 Azure 函数,只要“视频”容器中有视频文件,就可以将视频上传到我的 YouTube 频道。我使用的代码来自 YouTube API 示例代码:https://developers.google.com/youtube/v3/docs/videos/insert

我已获取此代码,并将其放在 Azure 函数中(代码如下)。当我在本地编译和运行该函数时,它运行良好,并且我可以使用 Azure 存储资源管理器上传视频,并且我看到视频可以正常上传到我的 YouTube 频道。

但是,当我将函数发布到 Azure 门户并运行该函数时,该函数只是超时,因为它需要一个 UI,因为它需要用户交互才能登录。您可能知道,Azure 函数不是一个交互式过程。

因此,为了克服这个问题,我想使用第一次在本地运行代码时创建的相同访问令牌文件,并将带有该令牌文件的函数再次上传到 Azure,以避免出现用户身份验证提示.但是访问令牌文件在我的%appdata% 文件夹中,所以我想将该文件移动到与我的代码相同的目录,然后读取该文件以获取访问令牌。

我有一个文件在我的

C:\Users\Peter\AppData\Roaming\Google.Apis.Auth\Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

我想将Google.Apis.Auth.OAuth2.Responses.TokenResponse-user(包含我的访问令牌)文件移动到与我的代码相同的目录并读取该文件。

问题:如何更改代码,使其读取文件与我的代码在同一目录中,而不是从%appdata%

我认为我应该能够使用此代码块 (https://www.daimto.com/google-net-filedatastore-demystified/) 来执行此操作,但我不确定它如何适合我现有功能的其余部分:

来自网站的代码块:

UserCredential credential;

using (var stream = new FileStream(clientSecretsJsonFilePath, FileMode.Open, FileAccess.Read))
{   
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
         GoogleClientSecrets.Load(stream).Secrets,
         new[] { DriveService.Scope.Drive,  DriveService.Scope.DriveFile },
         "LookIAmAUniqueUser",
         CancellationToken.None,
         new FileDataStore("credfolder", true)).Result;
}

这是我现有的全部功能:

    using System
    using System.IO;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Services;
    using Google.Apis.YouTube.v3;
    using Google.Apis.YouTube.v3.Data;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;

    namespace AzureFunctionToUploadToYoutube
    {
        public static class Function1
        {
            [FunctionName("Function1")]

            public static async Task Run([BlobTrigger("video/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, Microsoft.Azure.WebJobs.ExecutionContext context, ILogger log)
            {
                log.LogInformation("Function triggered by blob." + name);
                UserCredential credential;
                log.LogInformation("Getting client secrets.");

                using (var stream = new FileStream(System.IO.Path.Combine(context.FunctionAppDirectory, "client_secrets.json"), FileMode.Open, FileAccess.Read))
                {
                    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None
                    );
                }

                log.LogInformation("Done getting secrets.");
                log.LogInformation("Creating youtube service");

                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
                });

                log.LogInformation("Done creating service.");
                var video = new Video();
                video.Snippet = new VideoSnippet();
                video.Snippet.Title = name;
                video.Snippet.Description = "Seattle Channel";
                video.Snippet.Tags = new string[] { "tag1", "tag2" };
                video.Snippet.CategoryId = "22";
                video.Status = new VideoStatus();
                video.Status.PrivacyStatus = "unlisted";
                var VideoInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", myBlob, "video/*");

                log.LogInformation("Trying to upload video");

                try
                {
                    await VideoInsertRequest.UploadAsync();
                    log.LogInformation("Done uploading video.");
                }
                catch (Exception ex)
                {
                    log.LogInformation("Error uploading video: " + ex.Message);
                }
            }
        }
    }

【问题讨论】:

  • 您能否在此处补充说明您的问题所在。是读取文件的问题,还是将credential 应用于YouTubeServiceHttpClientInitializer = credential 不起作用吗?是否有错误等?
  • @John 我遇到的问题是,当我将函数发布到 Azure 时,它​​会超时,因为它正在等待用户进行身份验证。如您所知,Azure 函数不是一个交互式过程。所以为了克服这个问题,我将使用相同的访问令牌文件并再次上传该函数以避免用户身份验证。但是访问令牌文件位于我的 %appdata% 文件夹中,因此我想将该文件移动到与我的代码相同的目录中并读取该文件以获取访问令牌。
  • @John 问题/问题是我如何阅读“Google.Apis.Auth.OAuth2.Responses.TokenResponse-use”文件,一旦它与我的函数(代码)位于同一目录中.
  • 我明白了。您应该将所有这些信息编辑到您的问题中。我没有答案,但希望它能帮助其他人回答你:-)
  • @John 当然,谢谢

标签: c# asp.net .net azure .net-core


【解决方案1】:

您可以使用 Azure Blob 保存文件,并在每次运行该函数时检查该文件是否存在于 Blob 中。 如果该文件在 blob 上不存在,请创建它。 您可以在此处阅读有关从函数将文件保存到 blob 的信息: https://www.cyotek.com/blog/upload-data-to-blob-storage-with-azure-functions

【讨论】:

  • 不,这不能回答我的问题。我在问如何在代码中读取令牌文件。
  • 看起来 GoogleClientSecrets.Load 需要蒸汽,您可以将 Auth 文件复制到 azure blob 并从那里作为流读取
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 2017-04-16
  • 2015-02-08
  • 1970-01-01
相关资源
最近更新 更多