Search for messages in Google Chat Space through Google Vault API - java

I'm trying to create a Google Vault Export containing all messages in a Chat Space by querying for a specific Chat Space ID. Unfortunately, creating an export for the following query:
newQuery
.setDataScope("ALL_DATA")
.setCorpus("HANGOUTS_CHAT")
.setStartTime(startDate)
.setEndTime(endDate)
.setSearchMethod("ROOM")
.setHangoutsChatOptions(new HangoutsChatOptions().setIncludeRooms(false))
.setHangoutsChatInfo(new HangoutsChatInfo().setRoomId(spaceId));
produces the following error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
POST https://vault.googleapis.com/v1/matters/<matterID>/exports
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Search method ROOM is not supported for corpus type HANGOUTS_CHAT.",
"reason" : "badRequest"
} ],
"message" : "Search method ROOM is not supported for corpus type HANGOUTS_CHAT.",
"status" : "INVALID_ARGUMENT"
}
CorpusType needs to be set to "HANGOUTS_CHAT" in order to search in the Google Chat Service. The API also mentions that a HangoutsChatInfo() object needs to be passed when the search method is "ROOM" (https://developers.google.com/vault/reference/rest/v1/Query#SearchMethod).
Am I missing something or is this a bug? Does anyone know a workaround for this issue, if what I want to accomplish is not possible?

It seems that is intended behavior as per current design of the API since you will need to provide the corresponding RoomId and it's not possible at the moment.
Someone had a similar problem before and it ended up in a feature request, you can check it here: https://issuetracker.google.com/189250955

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

Write to GoogleSheet via API with Java

I am trying to write a value to a cell with Google Sheet API with Java.
For reading I used guide from Java Quickstart which worked fine for me.
For writing to Google Sheet I use:
service.spreadsheets().values().update(spreadsheetId, "Sheet1!A4:H", response).execute();
This function outputs the following error while run:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Request had insufficient authentication scopes.",
"reason" : "forbidden"
} ],
"message" : "Request had insufficient authentication scopes.",
"status" : "PERMISSION_DENIED"
}
As a Authentication Scope I am using
private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS);
Apparently there were several issues together:
Delete credentials that were stored at /Users/XXX/.credentials.
Change Scopes to SheetsScopes.SPREADSHEETS.
Google Sheet Share and Edit options at on Sheet itself.
Now it works!
Thank you guys for help
I was having the same issue. I resolved the problem that was in the scope.
I just changed
SheetsScopes.SPREADSHEETS.READONLY
To
SheetsScopes.SPREADSHEETS
And it works very well.
The Java API must be used in an interactive way, if you're running this on a server that can't pop up a web-browser (which will let you approve an OAuth dialog), then the authentication flow doesn't get proper credentials and won't work.
While running this, do you see a browser pop up to approve an OAuth dialog? If not, you're likely running in a headless session and will need some other means to get the user's credentials.
Try replacing "Sheet1!A4:H" with A4:H

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.

Why are the new profile scopes breaking requests to Google Classroom API in my app?

I have an application set up to access the Google Classroom API. I have it authorized and can pull Course and Roster data. It's updated to use the V1 calls, but when I add in the Profile.Name and Profile.Email scopes, I get the following error:
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Unregistered request was blocked. Please sign up using Google Developers Console.",
"reason" : "forbidden"
} ],
"message" : "Unregistered request was blocked. Please sign up using Google Developers Console.",
"status" : "PERMISSION_DENIED"
}
The service account has been authorized in the admin console with all 4 relevant Classroom scopes. The Domain has also been confirmed as having the Classroom APIs enabled, if I attempt to use them through the OAuth playground it all works fine.
Any ideas as to why I'm getting the error only when I add in the profile scopes?
The issue was that we were required to fill in a second form for Google to authorize the domain, we'd only found the first one. After filling in the new form, it all started working.

How to find user in google admin sdk

I'd like to find if user with certain username exists on my Gmail. So far I tried:
String query = "email='"+username+"#my.domain.com'";
users = directoryService.users().list().setQuery(query).execute();
But I only get:
13:47:12.654 [1651372403#qtp-1044945601-1] ERROR p.e.u.d.g.a.m.SDKGAManagementServiceImpl(153) - com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "badRequest"
} ],
"message" : "Bad Request"
}
I tried to query for a user with known username and I got desired response so I'm certain that the issue lies here. Could someone provide any help?
If you want to find the user using admin sdk, then you should give query like query=email=xxxx#domain.com. Also, even though its not mentioned in documentation, for users.list,you should give domain name too.
After giving the above parameters, I was able to successfully find the user. Let me know if you have questions.

Categories