I want to create an Azure function (C# API Generic HTTP) method that uploads a file to an Office365 Sharepoint document library.
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. The idea would be now to move the code into an Azure function. 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.
Best regards, Jens