Google Drive setWritersCanShare(false) and update of a File in JAVA - java

I'm trying to put the option "setWritersCanShare" of a file to FALSE. Now I am the owner of the file and I want that the file editors can't share the File. I can get the file and change its permission (via API) without problmes, but when I try to change the "setWritersCanShare" option, I get this response:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "The authenticated user does not have the required access to the file 0B0o1ZH_FIoLGUER2UFptYi1CVEU",
"reason" : "userAccess"
} ],
"message" : "The authenticated user does not have the required access to the file 0B0o1ZH_FIoLGUER2UFptYi1CVEU"
}
This is the file update code:
Drive service = new Drive.Builder(httpTransport, jsonFactory, null).setHttpRequestInitializer(credential).setApplicationName("La fiesta").build();
File file = service.files().get(fileID).execute();
file.setWritersCanShare(false);
File fileUpdate=service.files().update(fileID, file).execute();
What it's my error?
Thanks and regards

Try the operation in the Google API Explorer and see if the result is the same.
The error seems to indicate that the authenticated user isn't actually the owner of the file. Are you sure you're authenticating as the file owner (not a Service Account)?

Related

Missing a valid API key about Google Translation API Client issue?

I follow the https://cloud.google.com/translate/docs/reference/libraries#client-libraries-usage-java to get started java client demo.
I have already set authentication json file to the environment variable GOOGLE_APPLICATION_CREDENTIALS. However, I got the translateException when I run java sample code.
Exception in thread "main" com.google.cloud.translate.TranslateException: The request is missing a valid API key.
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:144)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:113)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:110)
Caused by:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403
Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "The request is missing a valid API key.",
"reason" : "forbidden"
} ],
"message" : "The request is missing a valid API key.",
"status" : "PERMISSION_DENIED"
}
The doc shows that this JSON file contains key infomation.
The sample code is shown
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
// The text to translate
String text = "Hello, world!";
// Translates some text into Russian
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
I have no idea how to set api-key.
It still doesn't work after I set environment variable for key and credentials.
you can try to authenticating by your service acount json file like below
its pretty simple in node
// Instantiates a client
const translate = new Translate(
{
projectId: 'your project id', //eg my-project-0o0o0o0o'
keyFilename: 'path of your service acount json file' //eg my-project-0fwewexyz.json
}
);
you can take the reference https://cloud.google.com/bigquery/docs/authentication/service-account-file for java
To make authenticated requests to Google Translation, you must create a service object with credentials or use an API key. The simplest way to authenticate is to use Application Default Credentials. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
Translate translate = TranslateOptions.getDefaultInstance().getService();
I have personally never gotten that to work.
This code can be also used with an API key. By default, an API key is looked for in the GOOGLE_API_KEY environment variable. Once the API key is set, you can make API calls by invoking methods on the Translation service created via TranslateOptions.getDefaultInstance().getService().
Sample project here
I was able to get google translate to work by running
"gcloud auth application-default login"
on the command prompt. This regenerated the credentials to the default location after asking you to authorize with your google account. See https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-translate for more details.
Add
System.setProperty("GOOGLE_API_KEY", "your key here");
before
Translate translate = TranslateOptions.getDefaultInstance().getService();
Cheers :)
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Requests from this Android client application <empty> are blocked.",
"reason" : "forbidden"
} ],
"message" : "Requests from this Android client application <empty> are blocked.",
"status" : "PERMISSION_DENIED"
}
Following Approach I follow in android to resolve it. I tried with Private key but did not work for me. so I use public key for this
System.setProperty("GOOGLE_API_KEY", "Public key");
val translate = TranslateOptions.getDefaultInstance().service
translate?.let {
val translation = it.translate("Hola Mundo!",
Translate.TranslateOption.sourceLanguage("es"),
Translate.TranslateOption.targetLanguage("en"),
Translate.TranslateOption.model("nmt"));
val check = translation.translatedText
Log.e("inf",""+check)
}

Able to access bucket in gcloud and Python API but not Java API

I'm working on a project that is includes a process to store files into Google Cloud Storage.
The initial prototype was in Python and works fine, however when porting the codebase over to Java I am unable to access the bucket.
The Java project is built with Gradle and development is through NetBeans. The ServiceAccount is a keyfile with restricted access, only allowing for read/write behavior from this particular bucket.
// The Java code, which should be enough to grab the bucket.
Storage storage = StorageOptions.getDefaultInstance().getService();
Bucket bucket = storage.get(BUCKET_NAME);
// For comparison, the Python setup.
storage_client = storage.Client()
self.bucket = storage_client.bucket(bucket_name)
When manually running the project from Gradle the result is,
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403
Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "xxx#xxx.iam.gserviceaccount.com does not have storage.buckets.get access to xxx.",
"reason" : "forbidden"
} ],
"message" : "xxx#xxx.iam.gserviceaccount.com does not have storage.buckets.get access to xxx."
}
Slightly different when running from within NetBeans itself:
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Anonymous users does not have storage.buckets.get access to a1s-submissions.",
"reason" : "required"
} ],
"message" : "Anonymous users does not have storage.buckets.get access to a1s-submissions."
}
All of these methods are returning the expected results
So I know at least the file is being read.
StorageOptions.getAppEngineAppId());
StorageOptions.getDefaultProjectId());
StorageOptions.CREDENTIAL_ENV_NAME);GOOGLE_APPLICATION_CREDENTIALS
StorageOptions.getDefaultInstance().getApplicationName());
StorageOptions.getDefaultInstance().getProjectId());
StorageOptions.getDefaultInstance().getCredentials()
.getAuthenticationType());
StorageOptions.getLibraryName());
So given that I can access the bucket via gcloud and python, I know the account has authorization, however I cannot seem to uncover the cause for this error.
Oddly, the solution here was in fact to relax the permissions on the service account. I didn't have access to this at the time but was finally able to check this today.
For whatever reason still unknown to me, Python was able to carry out the same operations with the original service account authorization settings while Java required additional rights granted.

How can I give full permissions to Google Drive REST API?

I'm trying to create an app that copies and deletes existing spreadsheets. I've set up the DriveQuickstart from https://developers.google.com/drive/v2/web/quickstart/java
The error that I'm getting now is saying that I do not have sufficient permissions to access that, even after I click the "Allow" button that comes up on my browser after I run the sample.
My code is exactly the same as on the quickstart link.
I'm getting this stacktrace:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at DriveQuickstart.main(DriveQuickstart.java:110)
To build on adjuremods response, this is what worked for me:
1) Change this line:
private static final List<String> SCOPES =
Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);
to:
private static final java.util.Collection<String> SCOPES =
DriveScopes.all();
2) Delete your existing stored credentials so that it will fetch them again, and reexecute your program:
rm ~/.credentials/drive-java-quickstart/StoredCredential
Note: you want an OAuth Client ID called client_secret.json file, not the client_id.json file (I think - that's what worked for me anyway).
Since you're using the exact code form the QuickStart, it may have something to do with the scope set, its currently set as DriveScopes.DRIVE_METADATA_READONLY, which if you check out its meaning in the Choose Auth Scopes page, it states
Allows read-only access to file metadata, but does not allow any access to read or download file content
You can look at the cited page to know what scope it is you'll exactly use. Its most likely going to be https://www.googleapis.com/auth/drive, so check out the DriveScopes class for the constant of the same nature.

Google Cloud Storage Java API - Deleting a file

How do I set a "WRITER" permission on a GCS bucket?
I am trying to delete a file on cloud storage using:
cloudstorage.objects().delete(bucket, object).execute();
and get the following error:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Forbidden",
"reason" : "forbidden"
} ],
"message" : "Forbidden"
}
With gsutil, like this:
gsutil acl ch -u email#address.com:W gs://bucket-name
Alternately, you can go to the Developer's Console, head over to storage > Cloud Storage > Storage browser, click on the "...", then "edit bucket permissions", then click "+ Add item", choose "user", the email address, and Writer, then click "Save".
Alternately, you can do this programmatically with the JSON API, using the storage.bucketAccessControls.insert method.

OAuth2 and Drive API, can't list/retrieve files

I have a web application that needs to list all files from my Google Drive and then fetch them when clicked.
I use OAuth for authenticating and it seems to work (the same code works well with Calendar API). I tried different scopes in serviceAccountScopes with no avail.
Basically authentication is:
credential = new GoogleCredential.Builder().
setTransport(HTTP_TRANSPORT).
setJsonFactory(JSON_FACTORY).
setServiceAccountId(apiEmail).
setServiceAccountScopes(DriveScopes.DRIVE).
setServiceAccountPrivateKeyFromP12File(p12File).build();
credential.refreshToken();
service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).
setApplicationName("My API").build();
edit: I should Add that credential's accessToken is null before refreshToken() call.
After that I try:
FileList files = service.files().list().execute();
The returned FileList is (should contain items):
{"etag":"\"_U9FTLXcHskmKgrWAZqJlfW8kCo/vyGp6PvFo4RvsFtPoIWeCReyIC8\"","items":[],"kind":"drive#fileList","selfLink":"https://www.googleapis.com/drive/v2/files"}
If I check that selfLink the contents is:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
Daily limit is not an issue here. (I guess this has nothing to do with this issue, since: https://stackoverflow.com/a/10639679/2090125). Also, I have enabled Drive and Drive SDK in Console (https://stackoverflow.com/a/10329353/2090125).
When downloading a file this is performed:
File file = service.files().get(fileId).execute();
And it produces this (fileId exists):
An error occured: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "File not found: 0B97KF40kTwrTaTllMnZCTV9ZSnM",
"reason" : "notFound"
} ],
"message" : "File not found: 0B97KF40kTwrTaTllMnZCTV9ZSnM"
}
And again when checking https://www.googleapis.com/drive/v2/files/0B97KF40kTwrTaTllMnZCTV9ZSnM the same "dailyLimitExceededUnreg" is seen.
What is going on here, is there a problem in my authentication? Should I configure Drive Integration in Drive SDK somehow? From Googles documentation I have understood that it's not necessary and the methods I'm using should work without further configuring.

Categories