Get list of files from SkyDrive folder (Windows Ph

2019-04-23 13:05发布

问题:

Does anyone know how to get a list of files for a particular SkyDrive folder? Currently I'm using the following snippet to try and get the files for the root SkyDrive folder:

var client = new LiveConnectClient(e.Session);

client.GetCompleted += (obj, arg) =>
     {
      ...
     }

client.GetAsync("me/skydrive");

but all it returns is a Result dictionary that contains a lot of info but no list of filenames!

回答1:

According to OneDrive core concepts (previously SkyDrive) you have two options to list files, either in the top directory or a specific folder. As you found out, you can list the top files using

liveClient.GetAsync("me/skydrive/files");

and for a specific folder you use folderId + "/files", for example

liveClient.GetAsync(folder.Id + "/files");

in the GetCompleted event you can list all files from the data key

private void onFilesInformationDownloaded(object sender,
                                          LiveOperationCompletedEventArgs e) {
    if (e.Result == null) {
        // check e.Error for reason why it failed
        return;
    }
    List<object> data = (List<object>)e.Result["data"];
    foreach (IDictionary<string, object> content in data) {
        string type = (string)content["type"];
        if (type == "folder") {
            // do something with folders?
        }
        string filename = (string)content["name"];
        string fileId = (string)content["id"];
        // use fileId to download a file or list files in a folder

        // there's a few more details available in content.Keys
        // such as created_time and updated_time for those interested
    }
}


回答2:

After getting desperate and asking the question here

it turns out the to get a list of files from the root skydrive folder you need to use the magic string me/skydrive/files rather than just me or me/skydrive



回答3:

It is really bad that MS does not document well the live content API.

  1. To get root folder contents use URI: https://apis.live.net/v5.0/me/skydrive/files?access_token=" + accessToken
  2. For any other folder contents use URI: https://apis.live.net/v5.0/folder.4ab680998d14f4e7.4AB680998D14F4E7!110/files?access_token=" + accessToken

Where folder.4ab680998d14f4e7.4AB680998D14F4E7!110 is the target folder you want to list.

Java code sample:

public void listRootFolder(String accessToken) {
    String folderId = "folder.4ab680998d14f4e7.4AB680998D14F4E7!110/files";
    String url = "https://apis.live.net/v5.0/" + folderId + "?access_token=" + accessToken;
    HttpMethod method = new GetMethod(url);
    HttpClient client = new HttpClient();
    try {
        int returnCode = client.executeMethod(method);
        System.out.println("Return code " + returnCode);
        System.out.println(method.getResponseBodyAsString());
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


回答4:

Are your files directly under "me/skydrive"? Otherwise you need to call it with client.GetAsync("me/skydrive/YOURFOLDER");

Then you data your output in a Result dictionary with the key data. and you can fetch it using this piece of code in your completedEvent handler:

       var data = (List<object>)e.Result["data"];
       foreach (IDictionary<string, object> content in data)
       {                   
           var skyContent = new SkyDriveContent();
           skyContent.Name = (string)content["name"];
           ContentList.Add(skyContent);    // where ContentList is :     List<SkyDriveContent> ContentList = new List<SkyDriveContent>(); in your class                
       }

Hope this helps.