I want to revoke permissions on a shared file on my Google Drive Account.
String fileId = "abc1234";
String permissionId = "abc1234";
try
{
service.permissions().delete(fileId, permissionId).execute();
}//try
catch (IOException e)
{
System.out.println("An error occurred: " + e);
}//catch
I think the problem here might be the permissionId's value. Does anyone know how can get the right one? Also, is this the right way to revoke someone's permissions on a file?
I would recommend doing a Permissions: list
service.permissions().list(fileId).execute();
This will return a list of the permission on the file in question. Then you can find the permission that you want to delete and run your delete on that.
Related
I'm trying to develop an app with Android Studio that creates a wallet file for the Ethereum Blockchain. I'm following this example.
I have two problems:
When I'm testing the app on my physical device, I can track down where is the JSON file, open it and see what is inside. On the Android Studio emulator I cannot find it.
Either on the emulator or physical device, the WalletUtils.loadCredentials function throws an error saying "some_filepath" (is a directory).
Code here:
public void createWallet(View view){
try{
WalletUtils.generateNewWalletFile(password, walletDir);
toastAsync("Wallet generated as" + walletDir);
}
catch (Exception e){
toastAsync("ERROR:" + e.getMessage());
}
}
public void getAddress(View view){
try {
Credentials credentials = WalletUtils.loadCredentials(password, walletDir.getAbsoluteFile());
toastAsync("Your address is " + credentials.getAddress());
}
catch (Exception e){
toastAsync("ERROR:" + e.getMessage());
}
}
I suppose loadCredentials can't reach the file, but I don't know why. I'm afraid the emulator actually doesn't create a file, because I couldn't find any, but on my physical device the file is there and loadCredentials still throws error
A File object in Java can represent both files and directories. I understand that walletDir File object is pointing to a directory where createWallet() method will store your credentials file. Then getAbsoluteFile() will return the same directory File object. You have to check the documentation on how to get a reference to the credentials file, or the path to it. Maybe WalletUtils.generateNewWalletFile() returns this information?
Due to some requirement I want to save text files files to Android's file system and read it anytime programmatically.
For each user who will be using the same phone there will be a different text file stored. Unfortunately, at that time when the user has not logged in the only unique information about the user I have is the email address (or is there anything else?).
So my question is can I use the email address as the filename for these .txt files such as "xyz_123#email.com.txt" since the email address can have multiple special characters which I'm not sure are allowed in filenames?
Try this .
1.create
String email = "xyz_123#email.com";
File file = new File(email + ".txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e("FILE_NAME", file.getName());
2.Add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.And you could use https://github.com/permissions-dispatcher/PermissionsDispatcher to request permission .
I've created a simple program that would write a file with a directory using the following codes:
String nameProve = nameField.getText();
String employee = ("C:\\Users\\ALLEN\\workspace32bit\\RETRIEVE_CHECKER1\\RETRIEVE_CHECKED1" + nameProve + ".txt");
PrintWriter outputStream1 = null;
try
{
outputStream1 = new PrintWriter(employeeName);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Can not write to " + employeeName);
System.exit(0);
}
outputStream1.println("Employee Retrieve Executed");
outputStream1.close();
I've already exported my code to a .jar file and my code works just fine if i execute it in the computer were I've develop my program but when I copied the jar file to other computer and I also created the forlder (manually) that corresponds with the directory in my codes my program doesn't work and my catch block will show the message Can not write to "C:\Users\ALLEN\workspace32bit\RETRIEVE_CHECKER1\RETRIEVE_CHECKED1" + nameProve + ".txt"
Can anybody give me some advice on how to solve this error? Thanks!
Obviously you are the user ALLEN on your machine and run the program as that user, so everithing works fine, because you are the owner of the directory C:\Users\ALLEN.
Then you copy the jar to another machine, where the user ALLEN does not exist and you are logged in as antoher user let's say his name is Bob and create that directory unter C:\Users. You may have noticed that you when you wanted to create that directory as the user Bob windows has warned you that you need admin priviledges in order to compleate this action.
Now you try to run your program with the user Bob who has access only to his own directory C:\Users\Bob and try to write to ALLEN's own directory. So what happens is that you get an IOException telling you access denied which is good so!
You should not attepmt to write to other users private direcotries, this is a security issue.
In your code when dealing with filesystem, never hard code absolute path, always use relative paths, or if you need to defined a direcotry where all the data needed yb you program should be located, then pass ist as argument.
The simplest to do, ist use the following and work with the current working directory
String employee = "RETRIEVE_CHECKER1\\RETRIEVE_CHECKED1" + nameProve + ".txt";
You need to create the directory RETRIEVE_CHECKER1 either by hand in location where you run the program, or better yet in your program using File#mkdir and use it like this:
File employee = new File(dir, "RETRIEVE_CHECKED1" + nameProve + ".txt");
PrintWriter outputStream1 = new PrintWriter(employeeName);
Exception you catch will have all details- like access denied, directory not exist, etc. Currently you loose exception data (message, stacktrace) when catching it.
Please do something like
Do not catch just java.lang.Exception. This is too wide- will catch possible NullPointerException and present misleading message '"Can not write to'. Catch only specific exception instead- ones that are actually thrown from try block. In your case, that will be java.io.FileNotFoundException
Do not loose exception details. Print it to the standard error log, or even better use loging framework
catch block that adresses those issues:
catch(FileNotFoundException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
I am creating simple function to check weather I have write/delete permission to a folder
I want to know whether the operating system will allow writing into the folder or not ?
public static String PermissionCheck(String FilePath) {
//File f = new File(FilePath);
String actions = "read,write";
try
{
AccessController.checkPermission(new FilePermission(FilePath, actions));
return ("You have read/write permition to use : " + FilePath);
}
catch (SecurityException e)
{
return ("You don't have read/write permition to use : " + FilePath);
}
}
When I call either with correct path or incorrect path the method it always returns message from cathc.
I know its duplicate question I have already gone through many links but no luck !!
Similar Question
Similar Question 1
Example :
PreChecks.PermissionCheck("C:/TEST/G2");
PreChecks.PermissionCheck("C:/Program Files/SAP");
Both calls return the message from catch block where I have all permission on "C:/TEST/G2" and no write permission on "C:/Program Files/SAP".
I have also tried canWrite but it says I have write permissions to "C:/Program Files/SAP" but I know I don't have those.
The class java.io.File is limited in its capabilities. E.g.
File f=new File("C:\\Program Files\\Java");
System.out.println(f.canWrite());
prints true on my machine though a user process is not allowed to write at this location.
In contrast,
Path p=Paths.get("C:\\Program Files\\Java");
System.out.println(Files.isWritable(p));
correctly prints false.
So the solution is to use the NIO API.
The method AccessController.checkPermission has an entirely different purpose. It helps implementing security managers. It throws a SecurityException because you don’t have an explicitly granted permission to access that directory, but as long as you don’t have a SecurityManager installed, that is irrelevant.
I would like to create a file which stores some data that can be accessed only by my app.Outside users should not be able to access this file or make any changes to it.I will store a key in the file which maybe accessed by the app whenever needed.
Use Environment.getDataDirectory(),
http://developer.android.com/reference/android/os/Environment.html#getDataDirectory()
This gives a File object that is the path to a private, app specific data directory. The files created therein are owned by your app's user ID, preventing any other app from accessing them.
File myPrivateFile = new File(Environment.getDataDirectory(), context.getPackageName() + File.separator + "secret.txt");
Note that if the device is rooted, all bets are off. A root process can read any file on the device. The best you can do is only store information for user of the device. That way if it's compromised, only one user account is compromised. In other words, don't store credentials, keys, access tokens, etc. That would allow a malicious agent access to a server where it could steal data for other users.
To create an application private file (that is not readable by other applications) you should use Context.openFileOutput() with a flag MODE_PRIVATE.
If you are concerned you could wrap the returned InputStream in CiperOutputStream and encrypt the contents.
If you are storing keys in a standard crypto format (X.509 or PKCS#12) you could use the new KeyChain API introduced in ICS.
Why not just stored it in a SharedPreference ??
Make your SharedPreference private to your activity by calling..
getSharedPreferences(yourfile, MODE_PRIVATE);
This will make sure only your Activity can access that SharedPreference.
To open a file from a private app folder, one can use this file path:
File myFilePath = new File(getContext().getFilesDir() + File.separator + "myFile")
You can check if the file exists if(myFilePath.exists()) ...
To save a text file to the private app directory
FileOutputStream fos = null;
try {
fos = getContext().openFileOutput("myFile",Context.MODE_PRIVATE);
} catch (FileNotFoundException e) { }
try {
fos.write(myTextString.getBytes());
} catch (IOException e) { }
try {
fos.close();
} catch (IOException e) { }
To save an image to the private app directory:
try {
myBitmapImage.compress(CompressFormat.JPG, 90,
openFileOutput("myimage.jpg", MODE_PRIVATE));
} catch (Exception e) { }
Your best bet is to store an encrypted file and decrypt it run-time, so even a rooted phone would not be able to read your data even if it has access to it. Of course, it's a chicken-egg condition (where would you store your key for the encrypted file), and the only thing better than putting the key in a variable is to generate that key runtime using some obscure method (see this)