c# - 使用 OneDrive API 的 Azure 函数返回编译错误

标签 c# onedrive azure-functions

我想创建一个将文件上传到 Office365 Sharepoint 文档库的 Azure 函数(C# API 通用 HTTP)方法。
因为 OneDrive API 允许我上传大文件(使用守护进程和证书身份验证),所以我成功地使用 C# 控制台应用程序实现了目标。现在的想法是将代码移动到 Azure 函数中。但是,我在保存/编译期间收到错误消息。

代码:

#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
}

编译时出错:

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'.

为什么会提示?这些函数在 .NET 4.6 上运行。

最好的问候, 延斯

最佳答案

作为替代方案,如果您想从 OneDrive 或实际上任何其他基于文件的 SaaS 提供商(例如 DropBox、GoogleDrive、Box...)读取/写入,您可能想尝试 SaaS 文件触发器、输入或Azure 函数中的输出。

这是一个 C# 示例:

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

关于c# - 使用 OneDrive API 的 Azure 函数返回编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192646/

相关文章:

c# - 如何在 Internet Explorer 中显示来自 BHO 的警告或栏?

c# - API 中的访问者模式有什么好处?

c# - 在 C# 中按行和按分隔符读取带分隔符的文本文件

android - 使用 LiveSDK 时出现 'redirect_url' 错误

c# - 没有隐式引用转换错误

microsoft-graph-api - 租户没有 SPO 许可证

c# - 使用 Graph API 将文件(> 4MB)上传到 OneDrive

.net - 隔离的 Azure 函数 Dockerfile

azure - Azure Function App 中的文件写入权限

azure - 如何使用 Azure 事件网格将多个存储帐户事件连接到一个函数