【问题标题】:Azure Function to get OAUth 2.0 token not working获取 OAUth 2.0 令牌的 Azure 函数不起作用
【发布时间】:2021-12-01 07:58:45
【问题描述】:

我正在学习本教程:

Access Google Analytics with Azure Data Factory

我正在努力解决的是 Azure 功能。

这是 .net / C# 代码:

using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Google.Apis.Auth.OAuth2;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using System;
using System.Threading.Tasks;
 
namespace GoogleAnalytics
{
    public class GetOAuthToken
    {
        [FunctionName("GetOAuthToken")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
        {
            var kvClient = new SecretClient(new Uri(Environment.GetEnvironmentVariable("KEY_VAULT_URL")), new ManagedIdentityCredential());
            string keyJson = kvClient.GetSecret("KEY_VAULT_URL").Value.Value;
 
            var cred = GoogleCredential.FromJson(keyJson).CreateScoped(new string[] { "https://www.googleapis.com/auth/analytics.readonly" });
            var token = await cred.UnderlyingCredential.GetAccessTokenForRequestAsync();
 
            return new OkObjectResult("{\"token\":\"" + token + "\"}");
        }
    }
}

我在 Azure 门户中使用以下参数创建了一个新的 Azure Function App:

发布:代码
运行时堆栈:.NET
版本:3.1
地区:德国

我点击代码 + 测试并将代码粘贴到窗口中。

如果我运行代码,我会收到以下错误:

2021-10-12T18:22:13  Welcome, you are now connected to log-streaming service. The default timeout is 2 hours. Change the timeout with the App Setting SCM_LOGSTREAM_TIMEOUT (in seconds).
2021-10-12T18:22:13.229 [Error] run.csx(3,7): error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.291 [Error] run.csx(7,42): error CS0234: The type or namespace name 'Http' does not exist in the namespace 'Microsoft.Azure.WebJobs.Extensions' (are you missing an assembly reference?)
2021-10-12T18:22:13.376 [Error] run.csx(17,14): error CS0246: The type or namespace name 'HttpTriggerAttribute' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.432 [Error] run.csx(17,14): error CS0246: The type or namespace name 'HttpTrigger' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.514 [Error] run.csx(17,26): error CS0103: The name 'AuthorizationLevel' does not exist in the current context
2021-10-12T18:22:13.572 [Error] run.csx(17,62): error CS0246: The type or namespace name 'Route' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.640 [Error] run.csx(19,32): error CS0246: The type or namespace name 'SecretClient' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.720 [Error] run.csx(19,111): error CS0246: The type or namespace name 'ManagedIdentityCredential' could not be found (are you missing a using directive or an assembly reference?)
2021-10-12T18:22:13.804 [Error] run.csx(22,24): error CS0103: The name 'GoogleCredential' does not exist in the current context
2021-10-12T18:22:13.826 [Warning] warning CS1702: Assuming assembly reference 'Microsoft.AspNetCore.Mvc.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' used by 'Microsoft.AspNetCore.Mvc.Core' matches identity 'Microsoft.AspNetCore.Mvc.Abstractions, Version=3.1.18.0, Culture=neutral, PublicKeyToken=adb12334449ddae60' of 'Microsoft.AspNetCore.Mvc.Abstractions', you may need to supply runtime policy
2021-10-12T18:22:13.850 [Error] Executed 'Functions.GetOAuth' (Failed, Id=9990a123-3fb8-4a3e-8346-260e03bb303a, Duration=1267ms)Script compilation failed.

我是否需要在我的 Azure 租户中安装任何包,还是需要事先做任何其他事情?

函数从哪里知道秘密以及使用哪个密钥库?我需要替换此代码中的占位符吗?教程在这方面不是很具体。

【问题讨论】:

  • 你安装了哪些 nuget 包?我的猜测是你没有,指南间接告诉你获取“Google.Apis.Oauth2.v2”。
  • 好的,我对 nuget 包一无所知。我想这就是问题所在。我会读到它。谢谢!

标签: c# .net azure oauth-2.0 azure-functions


【解决方案1】:

从您分享的教程来看,该过程看起来不错,查看您的堆栈跟踪,您导入的所有模块都丢失了。

确保您登录Visual Studio code 并导入所有包并添加 Azure Core 服务扩展、安装 Azure 功能扩展。

从本地测试函数并将其部署到您的函数应用。部署后,将所有本地设置添加到 Azure 函数应用门户的配置下的应用程序设置。

【讨论】:

  • 我在 Visual Studio 中创建了函数,添加了所有依赖项等,我可以将它作为函数部署在 Azure 中。唯一的问题是现在,我收到 HTTP 500 错误。我不知道为什么。
  • 这通常发生在 Azure 服务无法连接到其他服务时。请确保在配置中的应用程序设置中检查所有连接字符串是否有效。在您的情况下,您似乎正在使用密钥库存储连接,因此您需要在 KeyVault 配置中创建访问策略,以便 Azure 功能应用程序可以从密钥库中读取值。
  • 如果我在我的 Visual Studio 中本地运行函数的代码会发生什么?它应该连接到 Google 并检索令牌吗?我不确定代码是否正常工作。我在权利和访问政策等方面遇到了一些问题,但我想我现在明白了。我认为 HTTP 500 是 Google Analytics 服务器的响应。
  • 我检查了所有设置、策略等,这是我不断收到的错误:调用提供的 Azure 函数“GetOAuthToken”失败,状态为“InternalServerError”和消息——“调用 Azure 函数失败HttpStatusCode - InternalServerError.'.
  • "Value cannot be null" 是因为您没有指定与您的功能相关的内容。现在重新检查所有应用程序设置一次。我仍然怀疑 KeyVault 设置存在问题。确保使用托管身份对象在 KeyVault 配置中创建访问策略,并在 appsettings 中添加访问策略。参考这个:azure.microsoft.com/en-us/blog/…
猜你喜欢
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多