Google Drive SDK Update sharing permissions to private - java

I like to update the permission of a document from anyone with the link to private, to set the permission to (anyone with the link) use this and works:
newPermission.setType("anyone");
newPermission.setWithLink(true);
newPermission.setRole("reader");
but trying to set the permission to private is not working, i'm using this :
newPermission.setType("user");
newPermission.setValue("me");
newPermission.setRole("owner");
the response is this
500 Internal Server Error
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
What are the parameters that i have to pass to set the permissions to private ??

Are you the owner of the document? If not, you can not set yourself as owner. If you are, then the list of permissions for the document will have two entries, one for you as owner and another one to make everyone else a reader. Find the latter and delete it, and you should be all set.

Related

How to parse FirebaseAuthException exception message (Admin SDK Java)?

I'm using Firebase Admin SDK Java API v6.12.2.
I call FirebaseAuth.getInstance().generatePasswordResetLink(email, actionCodeSettings) to generate a password reset link for users. If the email isn't registered, I get a big blob of text with embedded JSON from e.getMessage().
The looked at the FirebaseAuthException doc and it only exposes one method e.getErrorCode(), which in this case returns internal-error.
I can certainly parse this text to look for "EMAIL_NOT_FOUND" and translate it into a user-friendly message. But isn't that a very clumsy error message? At least, there should have been methods to return the error code 400, and a simple String message, and the details could go into a JSON object.
What is the recommended approach here by the Firebase team and how are other developers handling it?
Output of e.getMessage():
Unexpected HTTP response with status: 400; body: {
"error": {
"code": 400,
"message": "EMAIL_NOT_FOUND",
"errors": [
{
"message": "EMAIL_NOT_FOUND",
"domain": "global",
"reason": "invalid"
}
]
}
}

Java - Google Drive API v3 - return specific fields for file - syntax

I am trying to get a large junk of Drive Files. I need to get the 3 Parameters emailAdress, displayName and modifiedTime. I only want to call the API once, to reduce processing time. However I cant figure out the syntax for the combined fields.
files = getDriveService().files().list()
.setFields("files, permissions(emailAddress,displayName)")
.setIncludeTeamDriveItems(true)
.setSupportsTeamDrives(true).execute();
Gives me:
"code": 400,
"errors": [
{
"domain": "global",
"location": "fields",
"locationType": "parameter",
"message": "Invalid field selection permissions",
"reason": "invalidParameter"
}
],
"message": "Invalid field selection permissions"
I`ve tried:
.setFields("files, permissions(emailAddress,displayName)")
.setFields("files(modifiedTime), permissions(emailAddress,displayName)")
.setFields("files/modifiedTime, permissions(emailAddress,displayName)")
.setFields("modifiedTime, permissions(emailAddress,displayName)")
.setFields("permissions(emailAddress,displayName)")
.setFields("modifiedTime")
Is it even possible to setFields() in combination with
.setSupportTeamDrives(true)
Thanks for your help.
A Permission refers to who can access a file. It is connected to a specific file.
Because of this, permissions is a field of the File resource, not a field in Files.list response, so it's normal that you're not getting the desired outcome. So you should access permissions as a field inside files. The request should be like this:
.setFields("files(modifiedTime, permissions(emailAddress, displayName))")
I hope this is of any help.

using google api explorer to upload data to google cloud storage

i am trying to use google api explorer to first try to insert an object to google cloud storage.
the request looks like
POST https://www.googleapis.com/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}
{
"contentType": "image/jpeg",
"uploadType": "media",
"path": "/upload/storage/v1/b/visionapibucket/o"
}
but i see the error as
400 HTTP/2.0 400
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required"
},
{
"domain": "global",
"reason": "wrongUrlForUpload",
"message": "Upload requests must include an uploadType URL parameter and a URL path beginning with /upload/",
"extendedHelp": "https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload"
}
],
"code": 400,
"message": "Required"
}
}
not sure what i am missing. please advise
Looks like a bug on the website. It doesn't seem like the explorer supports media.
The request it generated looks like:
POST https://www.googleapis.com/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}
But a proper upload request would look like:
POST https://www.googleapis.com/upload/storage/v1/b/visionapibucket/o?key={YOUR_API_KEY}&uploadType=media&name=myfile.jpeg
You'll also want to include a "Content-Type" header specifying that it's a JPEG image.
There's a guide on the various ways to upload objects using the JSON API here. The specific type you're looking for is like a simple upload.

Google Drive API: Change ownership on file not working (only writer/reader works)

I'm trying to changing a file to another user an make the new user the owner of an file , but this does not work.
I first of did a Permissions insert and added the newuser as writer, that was no problem. But I didn't work by adding the user as "owner" directly (Internal 500 error was returned)
Then I tried to use the Permission: list and saw the newuser as writer. Got that id of user and tried to use Permisson Update, and use the "transferOwnership" to the newuser.
But that don't work either, I always get the 500 Internal Server error.
Could this be anything that the newuser does not belong to the same email-domain that I as the owner belongs to?
If that is the reason, is there any other why to change the ownership to an external domain?
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZRkl82BYjpu
X-JavaScript-User-Agent: Google APIs Explorer
{
"role": "owner",
"type": "user",
"value": "newuser#officedomain.com"
}
This gives an error:
500 Internal Server Error
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
Edit:
Have found it possible to change ownership when both user have the same email-domain. But now to another issue.
When the ownership is moved to another used, the file is not shown in "shared files" sounds correct to me, but it is not shown in the directory structure either.
It is visible if I choose "show all files" and possible to search for if I specify the file name. But shouldn't it also be in the "My drive" at root ?
You are not able to add owner permission for user from another domain. You can only add owner permission for users, from the same domain to which current file owner belongs to.
I think we can solve it using below algorithm:
Add writer permission to source file for user from destination-domain.com. Permissions: insert. (Request to source-domain.com)
Now you can copy this file. Files: copy. (Request to destination-domain.com).
Copy permissions, comments and any other properties which you want.
I know, this is not the easiest solution but it works.

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