API to 'Disable downloading, printing, and copying of any Google Drive file' - google-drive-api

Google released this feature couple of months ago. The link below mentions that it is accessible via API. Tried using drive api patch call to toggle the "disable downloading .." flag which I assume is the "copyable" flag, but to no avail.
https://connect.googleforwork.com/docs/DOC-11088
Appreciate any help, thanks.

You're looking for the "labels.restricted" Property.
Here's what you would do it in C#.
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = Path.GetFileName(filename);
body.Labels = new Google.Apis.Drive.v2.Data.File.LabelsData();
body.Labels.Restricted = true;
byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request =
service.Files.Insert(body, stream, mimeType);
request.Upload();
}
catch(Exception ex){
ExceptionLogger.log(ex);
}
Check out the link below for samples.
https://developers.google.com/drive/v2/reference/files/copy.

labels.restricted is deprecated.
You should to set the copyRequiresWriterPermission metadata property instead.
Source: Google Drive API Files Reference

Related

How to send a file greater than 25 MB using Java Mail API

I am designing an application which sends Email with attachments using Gmail's smtp host. But when the file is larger than 25 MB, then I get an error saying that "552-5.2.3 Your message exceeded Google's message size limits. Please visit https://support.google.com/mail/?p=MaxSizeError to view our size guidelines.
188sm2692677pfg.11 -gsmtp"
final String username = "username#gmail.com";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("username#gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("receiver address"));
message.setSubject("This mail is a test mail");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Message");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = <Path>;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
} catch (MessagingException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
Is there any way of sending files greater than gmail's limit of 25 MB?
Can I change the file size upload limit from account settings or can I do something like any file will be uploaded as a drive link?
The user friendly way is probably to upload the file somewhere and add a link to the file in the mail.
It's also possible to split the file in several smaller parts and send each part in its own mail. The recipient then needs to join the files together again.
Zip-archivers can usually split large files into several zip-files that can then be join together again.
There's also raw splitting and joining. I haven't come across split commands built in to operating system distributions as standard. But your program could split the file in any way you desire.
Joining the files is then quite easy under Windows or Unix (Linux and others) operating systems. In Windows you go to the command prompt and use "copy": copy file1+file2+file3 finalfile In Unix you use "cat": cat file1 file2 file3 > finalfile
No. That is hard limit. Gmail itself states:
If your file is greater than 25 MB, Gmail automatically adds a Google Drive link in the email instead of including it as an attachment
That is what I would recommend for you as well, upload the file somewhere and paste a link in the mail. Options may be: Google Drive, Mega, Dropbox, S3, ...
Other than that there is nothing you can do.

GMail OAUTH not asking for permission when published

I've started using the GMail API and it's working fine on my local machine; it will open the Google permissions page and I can select my account. It then stores the return json token and only asks again if this token is removed.
When I publish to the server, the OAUTH page is never displayed and the application appears to timeout with a 'Thread was being aborted' exception.
My code;
try
{
using (var stream = new FileStream(HttpContext.Current.Server.MapPath("~/credentials/client_id.json"), FileMode.Open, FileAccess.Read))
{
string credPath = HttpContext.Current.Server.MapPath("~/credentials/gmail_readonly_token.json");
_credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
db.writeLog("INFO", "Gmail Credentials Saved","Credential file saved to: " + credPath);
}
// Create Gmail API service.
service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = _credential,
});
}
catch (Exception e)
{
db.writeLog("Error", "Failure when creating Gmail class", e.Message, null, _username, null);
}
Is there something I need to change within the 'client_id.json' (formally client_secret.json) file? The only thing I have altered is the redirect_uris line.
Any other suggestions would be welcome, the only other question I could find that is similar is here but there is no answer.
Thanks,
Danny.
The first one worked because you followed the intended use case, which is client-side. But, to implement authorization on the server, follow the Implementing Server-Side AUthorization guide.

Upload a file to Google Drive with embedded browser c#

Since I am unable to capture browser window close event using the GoogleWebAuthorizationBroker.AuthorizeAsync API, I followed this link (http://www.daimto.com/google-api-and-oath2/) to create an embedded browser and authenticate the user. I am unable to continue further to use the access token to upload a file in google drive. Is there any example available to continue from the above link to upload/download a file from Google Drive.
Regards,
Amrut
From the same author, there is a documentation how to upload/ download files to Google Drive.
Like with most of the Google APIs you need to be authenticated in order to connect to them. To do that you must first register your application on Google Developer console. Under APIs be sure to enable the Google Drive API and Google Drive SDK, as always don’t forget to add a product name and email address on the consent screen form.
Make sure your project is at least set to .net 4.0.
Add the following NuGet Package
PM> Install-Package Google.Apis.Drive.v2
In order to download a file we need to know its file resorce the only way to get the file id is from the Files.List() command we used earlier.
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
Using _service.HttpClient.GetByteArrayAsync we can pass it the download url of the file we would like to download. Once the file is download its a simple matter of wright the file to the disk.
Remember from creating a directory in order to upload a file you have to be able to tell Google what its mime-type is. I have a little method here that try’s to figure that out. Just send it the file name. Note: When uploading a file to Google Drive if the name of the file is the same name as a file that is already there. Google Drive just uploads it anyway, the file that was there is not updated you just end up with two files with the same name. It only checks based on the fileId not based upon the file name. If you want to Update a file you need to use the Update command we will check that later.
public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {
if (System.IO.File.Exists(_uploadFile))
{
File body = new File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File uploaded by Diamto Drive Sample";
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List() { new ParentReference() { Id = _parent } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
return request.ResponseBody;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
else {
Console.WriteLine("File does not exist: " + _uploadFile);
return null;
}
}

Set tags on BoxFile using Box Android SDK v2

I want to set tags on a file I am uploading using the Box Android Library v2. As far as I can tell you can only getTags from a file but there is no API method to set tags on upload or later. The only implementation of setTag that I found is in BoxItemRequestObject but this interface does not apply to BoxFile or BoxFileUploadRequest.
Below is what I would expect to be able to do, call setTag on the requestObject but there is no such api method available.
File file = new File(filePath);
BoxFileUploadRequestObject requestObject = BoxFileUploadRequestObject.uploadFileRequestObject(folderId.getFolderId(), file.getName(), file);
requestObject.setTag(myTag);
BoxFile boxFile = client.getFilesManager().uploadFile(requestObject);
I found the answer on their github repo.
BoxFileRequestObject requestObject = BoxFileRequestObject.getRequestObject();
requestObject.put(BoxFile.FIELD_TAGS, new String[] { "first tag", "second tag"});
boxClient.getFilesManager().updateFileInfo(fileId, requestObject);

Blackberry: Passing KML file to Google Maps

I want to know that can I pass KML as a string to google map application?
Code snippet:
//KML String
String document = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document><Folder><name>Paths</name><open>0</open><Placemark><LineString><tessellate>1</tessellate><coordinates> -112.0814237830345,36.10677870477137,0 -112.0870267752693,36.0905099328766,0</coordinates></LineString></Placemark></Folder></Document></kml>";
//Invoke Google Maps
int module = CodeModuleManager.getModuleHandle("GoogleMaps");
if (module == 0) {
try {
throw new ApplicationManagerException("GoogleMaps isn't installed");
} catch (ApplicationManagerException e) {
System.out.println(e.getMessage());
}
}
String[] args = {document}; //Is this possible???
ApplicationDescriptor descriptor = CodeModuleManager.getApplicationDescriptors(module)[0];
ApplicationDescriptor ad2 = new ApplicationDescriptor(descriptor, args);
try {
ApplicationManager.getApplicationManager().runApplication(ad2, true);
} catch (ApplicationManagerException e) {
System.out.println(e.getMessage());
}
After much R&D...the answer that I found out was that - No, we cannot pass KML as string.
Google Maps Mobile accepts a URL parameter. This needs to be in the form of "http://" and could be either specific parameters (such as "http://gmm/x?....") or a KML file ("http://..../sample.kml").
You MUST parse your mapFile (KML, gpx, txt) IF using the GoogleMaps API.
if all you want is to see your KML file inside a googlemaps window, you can goto the standard http://www.googlemaps.com and inside the ADDRESS box, you put the complete http address of your kml file.
until april2012 this used to work perfectly and the sidebar would be populated correctly, with clickable items (clicking on sidebar entry [e.g. "point 1" would fly-to the equivalent location). as of may2012, this feature is working incorrectly.
IF you want to take things one step further, you can have a PHP page echo the kml (on the fly KML rendering from database or file). BUT, googlemaps will not refresh your dynamically generated output for 10 to 15 minutes.
PM me if you want some javascript GPX and TXT mapFile rendering for GoogleMaps API vs3