"Mailbox does not exist." when fetching emails on EWS - java

I am trying to access emails on a Office365 mailbox, using Exchange Web Services (EWS).
My O365 admin has created :
the shared mailbox : shared#domain.com
the account : my.account#domain.com
a group, giving access to the account on the mailbox
I am able to retrieve the Oauth token using the appId/tenantId and a UserNamePasswordParameters using the account's credentials, and now I am trying to retrieve the emails from the mailbox, but I get this error :
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException:
Mailbox does not exist.
here's my code :
public Iterable fetchEmails(String token, String account) throws Exception {
if(token==null) {
token = getToken();
}
FindItemsResults<Item> emails;
try (ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)) {
service.getHttpHeaders().put("Authorization", "Bearer " + token);
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
service.setWebProxy(new WebProxy(PROXY_HOST, PROXY_PORT));
FolderId folder = new FolderId(WellKnownFolderName.Inbox, new Mailbox(account));
emails = service.findItems(folder, new ItemView(15));
}
return emails;
}

OK, it was a bit stupid.. I got confused because the account is also an email..
solution is to pass the mailbox (shared#domain.com) instead of the account (my.account#domain.com) when building the Mailbox object :
FolderId folder = new FolderId(WellKnownFolderName.Inbox, new Mailbox("shared#domain.com"));
And now it's working, I am able to get the emails.

Related

How to get an App Role's value with GRAPH API in Java

I am using GRAPH API via an application to connect to an AZURE AD. I am able to pull users/groups/roles. I can even pull app roles. However I need to be able to access the app roles value within my code and cannot figure out how to do so.
AppRoleAssignmentCollectionPage appRoleAssignments = graphClient.users(userId).appRoleAssignments()
.buildRequest()
.get();
List<AppRoleAssignment> memberRolesCurrentPage = appRoleAssignments.getCurrentPage();
if (memberRolesCurrentPage.isEmpty()) {
LOG.info("No app roles found for user");
} else {
ArrayList<String> roles = new ArrayList<String>();
for(AppRoleAssignment role : memberRolesCurrentPage) {
roles.add(role.principalDisplayName);
LOG.info( "ROLE: " + role.resourceDisplayName );
}
This is one way I am pulling data, but doing role."value" doesn't work and I can't figure out a way to get that data. Any suggestions?
Make sure you are already using below code at the starting of the code.
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
Here is an example of the request to retrieve the app roles that have been assigned to a user.
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
AppRoleAssignmentCollectionPage appRoleAssignments = graphClient.users("cdb555e3-b33e-4fd5-a427-17fadacbdfa7").appRoleAssignments()
.buildRequest()
.get();

How to list emails id of group or organization in exchange mail server using ews-java-api jar?

our company has exchange server and there are few users.So i want to list all users in our company and read emails of all users email one by one.How can i do that?
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); // your server version
ExchangeCredentials credentials = new WebCredentials("user.name#domain.com", "password!"); // change them to your email username, password, email domain
service.setCredentials(credentials);
service.setUrl(new URI("domain/EWS/Exchange.asmx"));
final int pageSize = 50;
ItemView view = new ItemView(pageSize);
FindItemsResults<Item> findResults;
do {
findResults = service.findItems(WellKnownFolderName.Inbox, view);
for (Item item : findResults.getItems()) {
// reading emails
}
view.setOffset(view.getOffset() + pageSize);
} while (findResults.isMoreAvailable());
With the above code i am able to get retrieve email of that particular account whose credentials i have provided in above code.But how can i list users emails id of company and retrieve emails of each user.Please help me with this.
Thanks in advance.

Gmail API returns failedPrecondition error when I try to send an email via my Google Suite Account

I have a GSuite account for my organization. I want to use Gmail API to send mail in my Java app but I don't want to use Oauth2 authent. I follow this documentation to delegate authority to a service account https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority.
I followed these steps:
configure a new account service
activate Gmail API in the dev console
associate rights between my client ID and the API
My Kotlin code to be able to initialize the Gmail service is below (the code is equivalent in Java)
#Configuration
class MixitApplication {
#Bean
fun jacksonFactory() = JacksonFactory.getDefaultInstance()
#Bean
fun dataStoreFactory() = MemoryDataStoreFactory.getDefaultInstance()
#Bean
fun httpTransport() = GoogleNetHttpTransport.newTrustedTransport()
#Bean
fun authorize(): Credential {
val jsonConfig = "{\n" +
" \"type\": \"service_account\",\n" +
" \"project_id\": \"mixit-EeAZEAZE\",\n" +
" \"private_key_id\": \"FAKE\",\n" +
" \"private_key\": \"-----BEGIN PRIVATE KEY-----\\FAKE\\n-----END PRIVATE KEY-----\\n\",\n" +
" \"client_email\": \"website#FAKE.iam.gserviceaccount.com\",\n" +
" \"client_id\": \"FAKE\",\n" +
" \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n" +
" \"token_uri\": \"https://accounts.google.com/o/oauth2/token\",\n" +
" \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n" +
" \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/website%40mixit-196820.iam.gserviceaccount.com\"\n" +
"}"
return GoogleCredential
.fromStream(jsonConfig.byteInputStream())
.createScoped(listOf(GmailScopes.GMAIL_SEND))
}
#Bean
fun gmailService() = Gmail.Builder(httpTransport(), jacksonFactory(), authorize()).build()
}
In my service when I try to end an email with this code
val result = gmailService.users().messages().send("me", emailMessage).execute();
I have always this error
400 Bad Request { "code" : 400, "errors" : [ { "domain" : "global", "message" : "Bad Request", "reason" : "failedPrecondition" } ], "message" : "Bad Request" }
I found several posts about similar problem. But it's often because the account is not a Gsuite account
I tried to find Google support for this problem but I had no solution for the moment. There' no information on the Google API console. I just know that I had a bad request.
My email function to send mail is very simple. EmailMessage is a custom object with email infos (to, subject and content)
fun send(email: EmailMessage) {
val session = Session.getDefaultInstance(Properties(), null)
val message = MimeMessage(session)
message.setFrom(InternetAddress("me"))
message.addRecipient(javax.mail.Message.RecipientType.TO, InternetAddress(email.to))
message.subject = email.subject
message.setContent(email.content, MediaType.TEXT_HTML_VALUE)
val buffer = ByteArrayOutputStream()
message.writeTo(buffer)
val emailMessage = Message()
emailMessage.encodeRaw(buffer.toByteArray())
val result = gmailService.users().messages().send("me", emailMessage).execute();
System.out.println(result.toPrettyString())
}
When I try to send this message by the test api console I https://developers.google.com/apis-explorer/?hl=en_GB#p/gmail/v1/gmail.users.messages.send the message is sent
Can you help me?
Using Gmail API requires OAuth2 authorization, even service accounts uses Oauth2.0:
All requests to the Gmail API must be authorized by an authenticated
user. Gmail uses the OAuth 2.0 protocol for authenticating a Google
account and authorizing access to user data. You can also use Google+
Sign-in to provide a "sign-in with Google" authentication method for
your app.
In short, you need to use OAuth2.0. You can check the implemenation of service account in this SO post.

Bing ads Campaign Management

I have recently started playing with the Bing Ads api for managing my ads and campaigns and I am having problem in authenticating user (not oauth authentication).
I authenticated my user using oauth by the following
private String devToken = "ZZZZZ";
private String clientId = "AAA0BBB-XXXX-AAAAA";
protected static String UserName = "a.v#h.c";
protected static String Password = "********";
// To get the initial access and refresh tokens you must call requestAccessAndRefreshTokens with the authorization redirection URL.
OAuthTokens tokens = oAuthDesktopMobileAuthCodeGrant.requestAccessAndRefreshTokens(url);
System.out.println("Access token: " + tokens.getAccessToken());
System.out.println("Refresh token: " + tokens.getRefreshToken());
authorizationData = new AuthorizationData();
authorizationData.setDeveloperToken(getDevToken());
authorizationData.setAuthentication(oAuthDesktopMobileAuthCodeGrant);
This authenticates my user just fine since I can use the ICustomerManagementService.class just fine for accounts related information
customerServiceClient = new ServiceClient<>(authorizationData, ICustomerManagementService.class);
ArrayOfAccount accounts = searchAccountsByUserId(user.getId());
The above works perfectly. But when I try to do the same with ICampaignManagementService.class like below
campaignServiceClient = new ServiceClient<>(authorizationData, ICampaignManagementService.class);
GetAdsByAdGroupIdRequest cReq = new GetAdsByAdGroupIdRequest();
cReq.setAdGroupId(1234567890L);
campaignServiceClient.getService().getAdsByAdGroupId(cReq);
I get error code 106 saying that the user is not authorized.
The user does not represent a authorized developer.
106
Any help in this regard ?
Please try to set the CustomerId and CustomerAccountId header elements (CustomerId and AccountId of AuthorizationData). These headers are not available with the Customer Management service, but are applicable for Campaign Management service. If that does not resolve the issue please feel free to send the SOAP request + response to support for investigation. I hope this helps!

Google Calendar API and OAuth problem

I get the error
com.google.gdata.util.AuthenticationException: Unknown authorization header
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530) ~[gdata-core-1.0.jar:na]
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535) ~[gdata-core-1.0.jar:na]
when trying to access the Google Calendar data via their API.
Here is what happens before that error.
1) I authenticate with Google:
final AccessTokenResponse response =
new GoogleAuthorizationCodeGrant(httpTransport,
jsonFactory,
clientId, clientSecret, authorizationCode,
redirectUrl).execute();
final GoogleAccessProtectedResource accessProtectedResource =
new GoogleAccessProtectedResource(
response.accessToken, httpTransport, jsonFactory,
clientId, clientSecret,
response.refreshToken);
LOGGER.debug("response.accessToken: {}", response.accessToken);
this.oauthAccessToken = response.accessToken;
...
2) I read some data via the tasks API:
this.service =
new Tasks(httpTransport, accessProtectedResource,
jsonFactory);
this.service.setApplicationName(this.applicationName);
This seems to work.
3) Then I try to read data from the Google Calendar API:
final OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
final GoogleOAuthParameters oauth = new GoogleOAuthParameters ();
oauth.setOAuthConsumerKey("myapp.com");
oauth.setOAuthConsumerSecret(CLIENT_SECRET); // Client secret from "Google API access" page, "Client secret" entry
oauth.setOAuthToken(this.oauthAccessToken); // Access token from step 1
oauth.setOAuthTokenSecret(aAuthorizationCode);
// aAuthorizationCode is taken from the callback URL.
// For http://myapp.com/oauth2callback?code=4/uy8Arb4bhRPwWYSr3QwKPt9lIZkt
// aAuthorizationCode is equal to "4/uy8Arb4bhRPwWYSr3QwKPt9lIZkt" (without quotes)
oauth.setScope(SCOPE_CALENDAR); // https://www.google.com/calendar/feeds/
final CalendarService calendarService =
new CalendarService(APPLICATION_NAME);
calendarService
.setOAuthCredentials(oauth, signer);
LOGGER.debug("calendarService: {}", calendarService);
final URL feedUrl =
new URL(
"http://www.google.com/calendar/feeds/default/allcalendars/full");
final CalendarFeed resultFeed =
calendarService.getFeed(feedUrl, CalendarFeed.class);
At the last line (calendarService.getFeed...) the aforementioned exception occurs.
I have following questions:
1) Is my call
oauth.setOAuthConsumerKey
correct?
I. e. is the "consumer key" equal to "Product name" in the Google API console, or to "Client ID" field (value is something like 42912397129473.apps.googleusercontent.com)
2) Is the setOAuthTokenSecret correct? I. e. is it the code that I get, when Google redirects the user back to my app?
3) If questions 2 and 3 were answered with yes, what else can be the cause of my problem?
Thanks
Dmitri
P. S.: Previously, I could access Google calendar with simple access (i. e. with Google user name and password). However, this is not an option now because users of my app will not want to give away their Google password.
Finally, I solved my problem by following the example at
http://code.google.com/p/gdata-java-client/source/browse/trunk/java/sample/oauth/OAuthExample.java
My advice to all future victims^W users of OAuth: Pay attention to the smallest details in the OAuth tutorials. The OAuth devil lies in details.

Categories

Resources