Skip to main content

Windows Live - SkyDrive

Windows LIVE series:
Astazi o sa vorbim despre cum putem accesa SkyDrive.
Pentru a putea avea acces la datele pe care un user le are pe SkyDrive este nevoie ca la logare sa setam scopul "wl.skydrive". Avand setat acest scope o sa putem accesa orice informatie pe care userul o are pe SkyDrive.
WL.init({ client_id: {idClient}, redirect_uri: {ourRedirectUrl} });
WL.login(
{ "scope": "wl.skydrive" },
function (response) {
if (response.status == "connected") {
alert("Utilizator conectat");
}
else {
alert("Autentificare esuata");
}
});
Dupa ce utilizatorul s-a logat tot ce ne-a mai ramas este sa apelam API-ul care il avem la dispozitie. Fiecare element care ne vine dupa o interogare o sa contina date precum tipul ementului( daca e folder sau fisier - ce tip de fisier), nume fisier, id, dimensiune etc. Aceste informatii sunt foarte asemanatoare cu cele pe care le avem cand suntem intr-un director de pe disk-ul local. Din JS pentru a aduce toate elementele din root trebuie sa facem urmatorul apel:
 WL.api({ path: ""/me/skydrive/files", method: "GET" }, onGetRootItemsComplete);
function onGetRootItemsComplete(response) {
if (response.error) {
alert("A aparut o eroare");
return;
}
var items = response.data;
var foundFolder = 0;
for (var i = 0; i < items.length; i++)
{
// item[i].type - tipul fisierului. Valore este "fo-lder" daca elementul este un folder
// daca ar fi un fisier aceast camp ar avea valoarea "file"
// item[i].name - numele la element
// item[i].id - id unic a elementului din SkyDrive
}
}

O sa avem nevoie de id, daca vrem de exemplu sa facem download la continutul unui fisier.
Pentru a putea vedea toate folderele, albumele de poze si fisierele din root a unui user pe de Skydrive e nevoie sa facem un request cu path-ul setat spre: me/skydrive/files
Pentru a accesa fisierele unui folder este nevoie sa apelam urmatorul path: {folderId}/files . Exact la fel se face daca vrem sa aducem pozele dintr-un albul: {album_id}/files.
Dar cum putem sa adaugam un nou folder pe SkyDrive. Pentru acest lucru avem nevoie de numele la folder, o scurta descriere si locatia unde dorim sa il adaugam. Mai jos puteti gasi un exemplu in C#:
Dictionary<string, object> newFolderData = new Dictionary<string, object>();
folderData.Add("NewFolderName", "New folder description");
LiveConnectClient liveConnectClient = new LiveConnectClient(session);
liveConnectClient.PostCompleted +=
new EventHandler<LiveOperationCompletedEventArgs>(CreateFolderPostCompleted);
client.PostAsync("me/skydrive/files/BaseFolder", newFolderData);
...
void CreateFolderPostCompleted(object sender, LiveOperationCompletedEventArgs e)
{
// Daca e.Error este diferit de NULL, atunci actiunea nu s-a terminat cu succes.
}
Daca in al doilea exemplu am vazut cum putem itera printr-un folder, acuma va propun sa download un fisier de pe SkyDrive. Odata ce avem id-ul la fisierul pe care vrem sa il copiem local, e nevoie sa apelam direct metoda download din JS sau DownloadAsync din C#. Pentru exemplul de mai jos am ales tot varianta C#:
client.DownloadCompleted +=
new EventHandler<LiveDownloadCompletedEventArgs>(OnFileDownloadCompleted);
client.DownloadAsync("file.{id_fisier}/content");
...
void OnFileDownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
{
// e.Result o sa contina fisierul nostru.
// Nu uitati sa faceti close la Result, deoarece este un stream: e.Result.Close();
}
Dupa cum am putut vedea prin intermediul API-ului este foarte simplu sa lucram cu SkyDrive. Nu uitati ca SkyDrive nu permite prin intermediul API-ului upload-ul la orice fel de fisiere. Asigurativa inainte sa faceti upload ca fisierul vostru este suportat. Din interfata web puteti uploada orice fel de fisiere.
Fisierele care se pot copia pe SkyDrive prin intermediul API sunt:
  • PDF, txt + fisiere Office precum Word, Excell, etc
  • poze in orice format supotat de Windows
  • audio doar in format wav
  • video in format wmv sau H.264
Orice tip de fisier poate sa fie adaugat din brower de exemplu. Trebuie tinut cont ca, chiar daca din API manipulam datele unui user( schimba vizibilitatea unui fisier), nu putem sa restrictionam accesul la fisere a.i. userul sa nu le poata accesa.

Comments

Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.
publ…