2016-12-06 63 views

回答

1

根據你的描述,我跟着這個avocado-console-client樣本,以測試它在我的身邊。我可以使它按預期工作,你可以參考下面的步驟:通過的NuGet

2.Modify功能authCtx.AcquireToken

1.Update Microsoft.IdentityModel.Clients.ActiveDirectory到最新的穩定版本(3.13.8),以authCtx.AcquireTokenAsync如下:

var authResult = await authCtx.AcquireTokenAsync(avocadoResourceUri, thisConsoleAppClientId, thisConsoleAppUri, new PlatformParameters(PromptBehavior.Auto)); 

3.Register您的本機應用程序,並修改thisConsoleAppClientIdthisConsoleAppUri

登錄到Azure Portal,單擊 「Azure的活動目錄」>「應用REG istrations」,單擊添加buttion添加您的本機應用程序如下:

對於thisConsoleAppClientId(您的客戶端應用程序ID),你會發現它在Azure門戶網站:

thisConsoleAppUri(您的應用程序的返回URL):

注:爲了訪問鱷梨API,您需要將您的應用程序與授權許可鱷梨分配。你可以按照下面的步驟:

選擇您在Azure AD應用程序,在設置葉片單擊「所需的權限」來添加權限,並選擇「完全訪問服務」選項。

注:你可以輸入Avocado [wsfed enabled]選擇鱷梨API。

一旦配置,你可以對它進行測試。這裏是我的代碼片段:

計劃。CS

class Program 
{ 
    static void Main(string[] args) 
    { 
     AvocadoDemo(); 
     Console.WriteLine("press any key to exit..."); 
     Console.ReadKey(); 
    } 

    static async Task AvocadoDemo() 
    { 
     string thisConsoleAppClientId = "c588cf8d-8651-4b37-8d10-49237cf92f8e"; 
     Uri thisConsoleAppUri = new Uri("http://bruceConsoleForAvocado"); 
     string avocadoResourceUri = "https://microsoft.onmicrosoft.com/Avocado"; 
     // Get the access token for Avocado. This will pop up the login dialog and consent page for the first time. 
     // Then the token will be cached in the FileCache, and AcquireToken will renew the token automatically in the subsequent run. 
     AuthenticationContext authCtx = new AuthenticationContext("https://login.windows.net/microsoft.onmicrosoft.com", new FileCache()); 
     var authResult = await authCtx.AcquireTokenAsync(avocadoResourceUri, thisConsoleAppClientId, thisConsoleAppUri, new PlatformParameters(PromptBehavior.Auto)); 

     if (!string.IsNullOrEmpty(authResult.AccessToken)) 
      Console.WriteLine("accessToken: " + authResult.AccessToken); 

     // call Avocado API 
     var baseAddress = new Uri("https://avocado"); 
     var cookieContainer = new CookieContainer(); 

     // POSTing a new execution will invoke a redirect, so for example purposes we are disabling it here. 
     using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer, AllowAutoRedirect = false }) 
     using (var client = new HttpClient(handler) { BaseAddress = baseAddress }) 
     { 
      // Need to set this cookie for avocado api to work. 
      cookieContainer.Add(baseAddress, new Cookie("deep_link", "-1")); 

      // add your access token in the header 
      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); 

      // Here's a GET example 
      var avoResultGet = client.GetAsync("/schedules/7215.json").Result; 
      var strGet = avoResultGet.Content.ReadAsStringAsync().Result; 
      Console.WriteLine("Avocado API returns: " + strGet); 
     } 
    } 
} 

結果

既然你已經通過驗證,你可以參考API Documentation,你想調用特定的API。