How to find user in google admin sdk - java

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.

Related

Search for messages in Google Chat Space through Google Vault API

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

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

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.

Get the Post Of User On Google Plus

I got a problem on getting the post of user on google plus. I only have the access token and refresh token of this user ( not the ID !), how could i get the post from google plus of this user ?, i do searching a lot, but none of them meets my circumstance. When the user provide his google plus Id on the front end side, the access token and refresh token were generated and stored in database, i, on the backend side, will use these tokens to retrieve the post of this user.
p/s: I use this library https://code.google.com/p/google-plus-java-api/ because it looks so simple, i don't want to use so complicated library just for getting the post, thanks you guy a lot
here is my code
import com.googlecode.googleplus.GooglePlusFactory;
import com.googlecode.googleplus.Plus;
import com.googlecode.googleplus.model.activity.ActivityCollection;
import com.googlecode.googleplus.model.activity.ActivityFeed;
public void getInfo(String accessToken, String refreshToken) {
GooglePlusFactory factory = new GooglePlusFactory(clientId, clientSecret);
Plus plus = factory.getApi(accessToken, refreshToken,null);
if (plus != null) {
ActivityFeed af = plus.getActivityOperations().list("me",ActivityCollection.PUBLIC);
String firstpost = af.getItems().get(0).getObject().getContent();
}
, i got 403 error when call this method, the error output was :
403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Daily Limit Exceeded. Please sign up",
"reason" : "dailyLimitExceededUnreg",
"extendedHelp" : "https://code.google.com/apis/console"
} ],
"message" : "Daily Limit Exceeded. Please sign up"
}
Actually, i think the reason is not because daily limit exceeded, because i got that message at the first time i run, i also turn the google+ api, google+ domain api on google api console. I suppose the error come from using the "me" in the "list" method to retrieve the post. I still dont' have any idea how to fix this problem
Updated #1#:
I have change my code a lot, so here is exactly what i stuck at, i already get a new accessToken, and (when it is still not expired) , i run this code below:
GoogleCredential credential = new GoogleCredential.Builder()
.setJsonFactory(JSON_FACTORY)
.setTransport(TRANSPORT)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET).build().setAccessToken(newAccessToken);
// Create a new authorized API client.
Plus service = new Plus.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
// Get a list of people that this user has shared with this app.
ActivityFeed feed = service.activities().list("me", "public").execute()
then i got this error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
Using "me" as the ID for the list call should work correctly, and your code generally appears correct.
The error message suggests that your client is unregistered. I would check to make sure of the following:
Make sure you have created a Client ID and Secret (and don't post them here!) for your project and you're passing them to the GooglePlusFactory correctly.
Make sure this project has the Google+ API turned on.
Make sure that the access token you're providing for the user corresponds to them authenticating to this same project
I solved this problem, everything is fine except 1 thing, that's is the access token and refresh token was created by some scopes but not the scopes Plus.me, that why's i can't request a new access token by using this refresh token, and then i got the insufficient permission error 403. So when i get the new access and refresh token by using the approriated scope, i can query with keyword "me". Hope you guy don't stuck like me, it takes me nearly 2 days to figure it out

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