C# Dropbox Api retrieve files of public shared folder - dropbox-api

i would like to ask, if there's any way to retrieve files links of folder which is publicly shared. Like someone create random public folder(everyone can view it) and put some random files into it. So i need to get all files links from that folder. All i know is link to that folder in format: https://www.dropbox.com/sh/[code]/[code].
Can i do that by using dropbox api, or the only option is to scrape dropbox page directly?

Here is a copy paste example:
using Dropbox.Api;
using Dropbox.Api.Files;
...
// AccessToken - get it from app console
// FolderToDownload - https://www.dropbox.com/sh/{unicorn_string}?dl=0
using (var dbx = new DropboxClient(_dropboxSettings.AccessToken))
{
var sharedLink = new SharedLink(_dropboxSettings.FolderToDownload);
var sharedFiles = await dbx.Files.ListFolderAsync(path: "", sharedLink: sharedLink);
foreach (var file in sharedFiles.Entries)
{
}
}
The documentation wasn't clear about setting path to an empty string when using it with publicly shared folders.

The official way to get information about a particular shared link is to use the Dropbox API's /2/sharing/get_shared_link_metadata endpoint:
https://www.dropbox.com/developers/documentation/http/documentation#sharing-get_shared_link_metadata
In the official Dropbox .NET SDK that's the GetSharedLinkMetadataAsync method:
https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Sharing_Routes_SharingUserRoutes_GetSharedLinkMetadataAsync_1.htm
This unfortunately doesn't offer the list of files though. We'll consider that a feature request.
Note that scraping the site would be error prone and likely to break without warning. (It's also against the terms anyway.)
Edit:
Dropbox API v2 now supports listing the contents of a shared link for a folder. This can be accomplished using the same interface as listing a folder in a connected user's account, via the list_folder functionality. To list the contents of a shared link for a folder, you instead provide the shared link URL in the shared_link parameter to /2/files/list_folder:
https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
If you're using an official SDK, there will also be a corresponding method for this endpoint. In the .NET SDK that's available as ListFolderAsync:
https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Files_Routes_FilesUserRoutes_ListFolderAsync_1.htm

Related

How to get a single file that has been shared with me on OneDrive?

I'm building an app that uses the ms-graph v1.0 API to write data to excelsheets in my OneDrive. It works with excel files that I uploaded to my drive but doesn't work with excel files that've been shared with me.
I know that I can get a list of all shared files with me/drive/sharedWithMe and the file that i want to edit is amongst the files that are being returned.
However, when i try to get one shared drive item using its driveItem property parentReference: driveID like this: /drives/{driveID}/items/{itemID} it returns : 403 - acces denied.
Here are my permissions:
"user.read",
"calendars.read",
"directory.accessasuser.all",
"files.readwrite.all"
I couldn't try the shares path /shares/{shareID} because I don't know how to figure out the shareId. It doesn't seem to be among the properties of the item that is returned by /sharedWithMe. Where can I get it?
Figured it out by myself.
I got the error
"message": "Cannot reference a user's drive from another user's personal site"
so I removed the me/from the route me/drives/{driveID}/items/{itemId} and it worked.

Google Cloud Storage getting download link

I'm working in an Asp.Net Core 2 web api for files hosted at Google Cloud Storage. The files hosted there are not public, so I can't use the MediaLink property of the object. I tried to make a download endpoint using MemoryStream but when there are many users downloading large files at once I run into memory issues.
My question is: is there a way to create something link a one-time download link for a file or something similar?
I'm also trying to implement what's described in this link but I'd need to give the bearer token to the user. I can't do that.
Any tips?
Yes. Google Cloud Storage offers a feature called "signed URLs" that is what you described: a URL that is only good for a short while to download a single file. The idea is that you craft a download URL, then use the private key of a service account to "sign" the URL. Anyone holding that final URL can use it to act as that service account for the purpose of downloading that one object.
Take a look: https://cloud.google.com/storage/docs/access-control/#Signed-URLs
Writing code to generate the signed URL is a bit tricky, but the client libraries provide helper methods in several languages to do it for you. You can also generate one with the gsutil command: gsutil signurl -d 10m privatekey.p12 gs://bucket/foo
There is a code sample for generating he signed URLs programatically on their GitHub project: Signed URLs
I managed to Create it using C#. I'm posting here because this will be useful to someone else:
1 - Create your private key
2 - Create and UrlSigner:
private readonly UrlSigner _urlSigner;
2 - In your class constructor:
using (var stream = File.OpenRead(_googleSettings.StorageAuthJson))
{
_urlSigner = UrlSigner.FromServiceAccountData(stream);
}
_googleSettings.StorageAuthJson has the physical path of the json file you downloaded when creating your key.
3 - Method to get the URL:
public string GetSignedUrl(string bucketName, string objectName, TimeSpan duration) {
var url = _urlSigner.Sign(bucketName, objectName, duration, null);
return url;
}

Downloading and Moving OneDrive files from shared link directory

I am looking for assistance to find out how I can download and move a OneDrive file that is accessed through a shared directory, via the shared link method of sharing.
I have two users:
user 'A' who is a Microsoft Consumer and has a regular OneDrive account and will host a csv file 'test.csv' in a folder 'toshare'
and user 'B' who is also a regular Microsoft Consumer who should use the graph API to download test.csv and then move the file to a subdirectory /toshare/archive
Aside: I am currently using the chrome app "advanced REST client" to manually make the REST calls, and am getting Authenticated OAuth BEARER tokens by inspecting network traffic from Microsoft's online "Graph Explorer" tool. After we understand the calls, we'll integrate it into our Java app.
I have succesfully followed the instructions here:
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/shares_get
to view the folder contents.
To be more explicit, user 'A' has went into OneDrive and has right clicked the folder 'toshare' and selected shareLink. I have converted the shareLink to a share token and then used the following API call with the Graph API as user 'B':
GET https://graph.microsoft.com/v1.0/shares/<share-token>/root?$expand=children
this shows me all the files in the directory, which includes 'test.csv'
Now, using this information, how can I download test.csv? Assuming user 'B' doesn't know the name of the file, but can identify it by being a .csv file (we can do this in code). There does not appear to be much documentation on how to download the files through a share.
The closest I've gotten was to take the "webUrl" attribute of the children object for my file, and then turn that into a share token and call
GET https://graph.microsoft.com/v1.0/shares/<child-share-token>/root
This will show me the file meta-data. and then I try to download it by roughly following the api documentation to download https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/item_downloadcontent
GET https://graph.microsoft.com/v1.0/shares/<child-share-token>/root/content
This is interesting because this works if I make the call with user 'A' but does not work for user 'B' who instead gets a 403 in advanced REST client. (If I run it in Graph Explorer, I get "The site in the encoded share URI is invalid." instead, which I've discovered with other experimentation, really means there's an authorization issue.)
GET https://graph.microsoft.com/v1.0/shares/<share-token>/root:/test.csv:/content
Also does not work, it returns: "400 Bad Request" with message: "Resource not found for the segment 'root:'." It seems like the path style file navigation does not work for shared directories?
At this point I'm rather stuck. After downloading the file, I also would like to move it into a subdirectory, denoting that it has already been read in. I'd also like to get this working for OneDrive for Business, but that seems to be another set of challenges that I'll leave for another day.
Any insight would be great thanks,
Jeremy
It's best to consider the shares/{id} segments to be similar to drives/{id}, at which point all of the previous documentation around children access is applicable. Given your scenario I'd use the path syntax:
https://graph.microsoft.com/v1.0/shares/<share-token>/root/children/test.csv
This obviously necessitates knowing the file name, but it sounds like you already have an algorithm to do that.
Theoretically your approach for creating a child-share-token would work, but it would now require that User B both provide authentication as well as to have explicit permissions. Since your share-token was a sharing link User B is most likely getting permission by virtue of the fact that they have the URL, in which case generating a new one is probably removing the special token that allows this to work. That's why it's best to always use the original share-token where possible.
Similar rules will apply to move the file. First off, we'll assume that the sharing link provides the ability to "Edit" otherwise none of this will work :). Second, we'll assume that the archive folder already exists (if it doesn't you'd need to create it using a POST to https://graph.microsoft.com/v1.0/shares/<share-token>/root/children that looks like what we've documented here).
To move the file you'd want to PATCH to https://graph.microsoft.com/v1.0/shares/<share-token>/root/children/test.csv and provide a new parentReference as documented here. It's always best to use id values if you have them, but you should also be able to provide the path to the parent in the form of /shares/<share-token>/root/children/archive.

Dropbox API: How to use API to get file's shared link?

I have a folder which contains 100+ files, I want to shall them all and get all the shared links, is there anyway to do this?
That's DropBox API v. 1, which is now deprecated:
https://blogs.dropbox.com/developers/2016/06/api-v1-deprecated/
I found, for current v.2, depending on your needs:
/2/files/get_temporary_link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
/2/sharing/create_shared_link_with_settings https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
/2/sharing/create_shared_link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link
You can use the /shares endpoint to get shareable links to any file or folder.

How to show images in other domains / Chrome Packaged Apps

I have a JSON which returns a list of URLs of images to access the JSON that is already placed in a field in this domain whitelist manifest.json, however when I try to view the pictures it complains that it can not access the images.
1 - How to Perm can display images that are not within the package App
2 - How can I download the images to download the APP and then display
to this second question I used the RAL, a lib trial google and it worked, however I could not make a test using this publication lib he claims an error, follow the link to the image lib and complaining about the error:
lib: https://github.com/GoogleChrome/apps-resource-loader
Error: http://twitter.yfrog.com/oe24998653p
You can request external images using XMLHttpRequest and transform them into ObjectURLs. Then set the src attribute in the <img> tag to each ObjectURL and it should work.
Since this is a very common use case, we created a library to simplify it. Just drop the apps-resource-loader ral.min.js to your project and then:
var remoteImage,
container = document.querySelector('.imageContainer'),
toLoad = { 'images': [
'http://myserver.com/image1.png',
'http://myserver.com/image2.png' ] }; // list of image URLs
toLoad.images.forEach(function(imageToLoad) {
remoteImage = new RAL.RemoteImage(imageToLoad);
container.appendChild(remoteImage.element);
RAL.Queue.add(remoteImage);
});
RAL.Queue.setMaxConnections(4);
RAL.Queue.start();
Remember that you need permission in the manifest.json to all domains you will be XHR'ing to. If you don't know beforehand where those images will be hosted, you can ask permission for any url:
permissions: ['<all_urls>'],
For other usages and to get the full library, please see the project page:
https://github.com/GoogleChrome/apps-resource-loader
and a simple demo at:
https://github.com/GoogleChrome/apps-resource-loader/tree/master/demo
Google has added a browser tag that allows you to include images, but so far I haven't been able to get it to work.
Here is what they show as an example
<browser src="http://i.stack.imgur.com/dmHl0.png">