简体   繁体   English

使用OneDrive API的Azure功能会返回编译错误

[英]Azure Function consuming OneDrive API returns compile errors

I want to create an Azure function (C# API Generic HTTP) method that uploads a file to an Office365 Sharepoint document library. 我想创建一个Azure函数(C#API通用HTTP)方法,将文件上载到Office365 Sharepoint文档库。
Because OneDrive API allows me to upload large files (using daemon process & certificate auth.), I have succeeded in achieving the goal with a C# Console Application. 由于OneDrive API允许我上传大文件(使用守护程序进程和证书身份验证),因此我成功地使用C#控制台应用程序实现了目标。 The idea would be now to move the code into an Azure function. 现在的想法是将代码移动到Azure功能中。 However, I receive an error during save/compilation. 但是,我在保存/编译期间收到错误。

Code: 码:

#r "Newtonsoft.Json"

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

public class DriveResult
{
    [JsonProperty("@odata.id")]
    public string id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

const string appId = "<myAppId>";
const string tenantName = "<tenantName>";
const string tenantId = "<myTenantId>";


private static string GetResourceUrl()
{
    return "https://" + tenantName + ".sharepoint.com";
}

private static string GetAuthority()
{
    return "https://login.windows.net/" + tenantId + "/oauth2/authorize";
}

public static async Task<bool> Run(HttpRequestMessage req, TraceWriter log)
{
    var token = await GetAccessTokenAsync();
    return true; //temporary code
}

private static async Task<string> GetAccessTokenAsync()
{
    AuthenticationContext authenticationContext = new AuthenticationContext(GetAuthority(), false);
    string certfile = @"mykeyfile.pfx";

    X509Certificate2 cert = new X509Certificate2(certfile, "<myinsanepwd>");

    ClientAssertionCertificate cac = new ClientAssertionCertificate(appId, cert);
    var authenticationResult = await authenticationContext.AcquireTokenAsync("GetResourceUrl()", cac);
    return authenticationResult.AccessToken;
}

Project.json
{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.5",
          "System.Security.Cryptography.X509Certificates": "4.1.0"
      }
    }
   }
}

Function.json
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

Errors during compilation : 编译期间的错误:

2016-10-22T13:05:24.625 run.csx(50,60): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
2016-10-22T13:05:24.625 run.csx(50,38): error CS0012: The type 'Task<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Why is it complaining? 为什么抱怨? The functions runs on .NET 4.6. 这些函数在.NET 4.6上运行。

Best regards, Jens 此致,Jens

As an alternative, if you want to read/write from/to OneDrive or in fact any other file based SaaS provider such as DropBox, GoogleDrive, Box, ... you may want to try the SaaS File trigger, input or output in Azure functions. 作为替代方案,如果您想要读取/写入OneDrive或实际上任何其他基于文件的SaaS提供程序(如DropBox,GoogleDrive,Box等),您可能希望在Azure中尝试SaaS文件触发器,输入或输出职能。

Here is a C# sample: 这是一个C#示例:

https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp https://github.com/Azure/azure-webjobs-sdk-templates/tree/dev/Templates/SaaSFileTrigger-CSharp

Jens, 延,

This is an issue triggered by some cases when referencing PCLs. 这是引用PCL时某些情况触发的问题。

We have a plan for a more permanent solution, but in the meantime, you should be able to compile your function by adding explicitly references to the assemblies you need as you've done with JSON.NET. 我们有一个更永久的解决方案的计划,但与此同时,您应该能够通过在使用JSON.NET时显式引用所需的程序集来编译您的函数。 So in your case, the following: 所以在你的情况下,以下内容:

#r "System.Runtime"
#r "System.Threading.Tasks"

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM