【发布时间】: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