2016-10-22 6 views
1

ファイルをOffice365 SharepointドキュメントライブラリにアップロードするAzure関数(C#API Generic HTTP)を作成します。
OneDrive APIは大きなファイル(デーモンプロセス&証明書認証を使用して)をアップロードできるので、私はC#コンソールアプリケーションで目標を達成することに成功しました。アイデアはAzure関数にコードを移動することになります。ただし、保存/コンパイル中にエラーが発生します。Azure関数OneDrive APIを使用してコンパイルエラーを返します

コード:コンパイル中

#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で動作します。

敬具、 イェンス

答えて

1

イェンス、

これは、のPCLを参照する際一部例によりトリガー問題です。

もっと恒久的な解決策がありますが、その間に、JSON.NETで行ったように必要なアセンブリに明示的に参照を追加して関数をコンパイルできるはずです。あなたの場合、次のようになります:

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

+0

こんにちはファビオ、 これが実際にコンパイルエラーを解決します。ただし、 – Jens

+0

行 X509Certificate2証明書=新しいX509Certificate2(certfile、 ""); が例外をスローします。System.Security.Cryptography.CryptographicException:指定されたファイルが見つかりません。 ファイルが指定されたパス上に存在するため、実際には奇妙です(File.Exists()を使用して確認しました) P.S. Azure関数の現在のディレクトリがD:\ Windows \ system32に設定されていることがわかりました – Jens

+0

次のように作成された絶対パス(ファイル名ではなく)を渡すことができますか:System.IO.Path.Combine(Environment .ExpandEnvironmentVariables( "%HOME%")、@ "site \ wwwroot \ \ mykeyfile.pfx"); 私はGitHubで問題を開いていますので、より便利な方法で関数ディレクトリを取得することができます。 –

関連する問題