2016-05-17 18 views
1

我遇到了一個奇怪的問題,試圖通過C#訪問存儲在O365視頻上的縮略圖。我可以在沒有任何問題的情況下訪問REST API,我只需將Authentication: Bearer <token>添加到標題中,然後關閉運行。麻煩在於我從特定視頻中獲得的基本圖片網址。在O365視頻中使用C#時出現故障downloadig縮略圖圖片

https://<mytenant>.sharepoint.com/portals/Channel1/pVid/myvideo.mp4.PNG?VideoPreview=1 

當我從瀏覽器訪問該網址時,它100%的工作時間。當我嘗試通過httpclient對象訪問它時,出現401 Unauthorized錯誤。

我能想到的最好的方式是訪問基本URL時,授權標頭令牌不受尊重。這使我需要其他東西,比如cookie?但我似乎無法弄清楚哪一個。尋找任何建議:)

回答

1

而不是通過憑據,是的你需要一個身份驗證cookie。下面是一個示例:

private static async Task<string>getWebTitle(string webUrl) 
{ 
//Creating Password 
const string PWD = "softjam.1"; 
const string USER = "[email protected]"; 
const string RESTURL = "{0}/_api/web?$select=Title"; 

//Creating Credentials 
var passWord = new SecureString(); 
foreach (var c in PWD) passWord.AppendChar(c); 
var credential = new SharePointOnlineCredentials(USER, passWord); 

//Creating Handler to allows the client to use credentials and cookie 
using (var handler = new HttpClientHandler() { Credentials = credential }) 
{ 
    //Getting authentication cookies 
    Uri uri = new Uri(webUrl); 
    handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri)); 

    //Invoking REST API 
    using (var client = new HttpClient(handler)) 
    { 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false); 
     response.EnsureSuccessStatusCode(); 

     string jsonData = await response.Content.ReadAsStringAsync(); 

     return jsonData; 
    } 
} 

}

+1

謝謝!但是,由於我使用O365 oAuth和Federation模型(使用O365登錄頁面),我沒有證書。我認爲我需要的是獲取OAuth令牌並將其「轉換」爲FedAuth cookie的方式。 – Shawn