Reputation: 67
I am following this tutorial:
Access Google Analytics with Azure Data Factory
What I am struggling with is the Azure function.
This is the .net / C# code:
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 + "\"}");
}
}
}
I create a new Azure Function App in Azure Portal with the following parameters:
Publish: Code
Runtime Stack: .NET
Version: 3.1
Region: Germany
I click on Code + Test and paste the code into the window.
If I run the code I get this errors:
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.
Do I need to install any packages inside my Azure tenant or do I need to do anything else beforehand?
And from where does the function know with secret and which key vault to use? Do I need to replace a placeholder within this code? The tutorial is not very specific in this regard.
Upvotes: 0
Views: 1454
Reputation: 1301
The procedure looks good from the tutorial which you have shared, looking at your stack trace all the modules which you have imported were missing.
Make sure you sign-in to Visual Studio code and import all the packages and add Azure Core service extension, Install Azure functions extension.
Test the function from the local and deploy it to your function app. After deployment add all the local settings to application settings under Configurations in Azure function app portal.
Upvotes: 1