Read the contents of a .csv file in Google Drive - flutter

I have a public .csv file on my Google Drive and I want to pull the file and display it in my flutter app. I figured that I need the fileID, the OAuth token and the client ID,I already got these. I also have a fully working sign-in page. But I can't figure out what is the exact methodology of fetching the file. I know that there is some content of this exact question online, I am a beginner, those posts were old, and some functions were deprecated, I couldn't understand them.

You can use the export api to get this
"https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"
Refer https://developers.google.com/drive/api/v3/reference/files/export for more details
Please note that is restricted to 10 Mb per file
Edit
add these dependencies
dependencies:
googleapis: any
google_sign_in: any
Implement google signing
final googleSignIn = signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.DriveScope]);
final signIn.GoogleSignInAccount account = await googleSignIn.signIn();
Create a firebase project and enable google signin and link it with your project. Download its Json and add it to the project too.
Goto cloud console and enable Google Drive API
After the login is complete you can use the download method to download the file
Future<File> download(String id, String filename, {Function(int, int) onDownloadProgress});
Check this tutorial for reference
https://betterprogramming.pub/the-minimum-guide-for-using-google-drive-api-with-flutter-9207e4cb05ba

Related

Flutter Firebase Storage Add additional parameters to end of download url

I have an issue regarding the integration of downloaded photos from Firebase storage in my Flutter app.
I am using the https://pub.dev/packages/gallery_saver package to download the images to the device from the user. Due to a bug/the concept of the plugin you are only able to download images if the url ends with e.g. jpeg/png/jpg etc.
Here some other comments of people who have the same issue withe the package: https://github.com/CarnegieTechnologies/gallery_saver/issues/66
To use the package with my app now I am adding the filename to the end of the image url.
This works completely fine when my security rules allow all reads.
As soon as I add these rules:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if request.auth != null;
}
}
}
I get an error 403 forbidden on downloading all images.
Is there a way to make this work while using file endings at the end of the url?
Download URLs generated by Firebase for Cloud Storage are opaque URLs, and you can't modify them.
Your options are to:
Mark the file as public on Cloud Storage itself, so that you can download it without the extra token in the URL.
Fix the plugin you use to allow downloading without a filename extension, in which case it can determine the file type from its metadata.
Expand the plugin you use to allow downloading the file through the Firebase SDK, instead of through a download URL.

How to make user download/save file from the flutter app?

I want user to be able to store txt/doc/pdf file generated by flutter app wherever he/she wants on his/her phone. For example, if it were a webapp, this would be achieved by sending HTTP header Content-Disposition: attachment; filename=MyFileName.txt. Is it possible at all in flutter application? I tried looking at url_launcher, flutter_downloader, flutter_webview_plugin or flutter_web_browser packages, but non of them offer relevant functionality. I would appreciate any tips regarding this issue.
Getting the directory
You can use flutter_file_dialog to get the user the choose the location with this code:
final params = SaveFileDialogParams(sourceFilePath: "path_of_file_to_save");
final filePath = await FlutterFileDialog.saveFile(params: params);
print(filePath);
Saving the file
For that just use path_provider : https://flutter.dev/docs/cookbook/persistence/reading-writing-files

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;
}

C# Dropbox Api retrieve files of public shared folder

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

Downloading files from google drive with rest API / c++

I have files in my google drive , very small ones, <10kB, and I try to download those by using downloadUrl , but the result only seems to be redirection to happen all the time. I have seen also discussions about that but no actual results ..
I am using c++ and creating rest API request by myself ( not using any libraries for this )
But also , when I copy the url I receive after continue="..." web browser can make a download okay but calling that from C++ seems to cause just another redirection to happen .
I followed the instructions from:
https://developers.google.com/drive/web/manage-downloads
This is probably a duplicate of Unable to retrieve file content from google drive API which states that the access token for downloading content needs to be set as an HTTP header.