I want to read my server gmail account email from java application - spring-boot

I need help in understanding the exact path I should follow to solve my problem.
I hava a gmail address(I can say this gmail is used by my java application) Whenever a mail comes to this gmail address. I want my application to know and get the email from my gmail inbox and proceed with the further logic.
So far Going through the google docs I have created a topic and a subscribe which has subscribed to that topic. Using push mechanism I'm also able to call my endpoint whenever a message get published manually from google console to that topic.
Now I want if any email comes to my gmail address, it should notify my endpoint and after I can just fetch the email from the gmail and proceed with the logic.
Here As I have only mine gmail address which I will be accessing I don't think I need to go for Oauth2 mechanism for gmail authentication purpose.
I went through the documentation which says. I should create a watch request, then maybe if any gmail inbox receives a mail it will notify my endpoint and then I can fetch the mail and proceed further. But I'm not sure what and how exactly I should proceed Do I need a service account if yes I have already created a service account and downloaded my private key.
GoogleCredential.fromStream(new FileInputStream("file.json"))
.createScoped(Collections.singleton(GmailScopes.MAIL_GOOGLE_COM)).createDelegated("xxxxx.iam.gserviceaccount.com");
then I'm calling watch request as :
Gmail gm = new Gmail.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, googleCredBuilder()).build();
WatchResponse res = gm.users().watch("me", request).execute();
I got this error.
POST https://www.googleapis.com/gmail/v1/users/me/watch
{
"code": 400,
"errors": [
{
"domain": "global",
"message": "Precondition check failed.",
"reason": "failedPrecondition"
}
],
"message": "Precondition check failed.",
"status": "FAILED_PRECONDITION"
}
Can someone please help how exactly I should proceed to achieve my goal.
Thank you.

"Precondition check failed.",
Means that you are trying to use a service account with a standard google gmail address.
Service account authorization only works with gmail api and google workspace accounts once domain wide delegation has been configured.
You have two options
swtich to using a google workspace domain account.
Use Oauth2, set your application to production and not test so that the refresh token wont expire. Authorize your application once store the refresh token, then upload it to the server. It will be able to use the refresh token to request a new access token whenever it needs.

Related

Google Calendar API request is denied for scope https://www.googleapis.com/auth/calendar.app.created

My app needs to create a Google Calendar and manage events in it for users. Google API Console allows to create a consent form for this scope:
https://www.googleapis.com/auth/calendar.app.created
It is supposed to allow creation of secondary calendars and events in those calendars. Description reads:
Make secondary Google calendars, and see, create, change, and delete
events on them
However, after a user gives permission through the consent form etc, I am getting PERMISSION_DENIED when I try to create a calendar (I am using the REST api https://www.googleapis.com/calendar/v3/calendars)
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"errors": [
{
"message": "Insufficient Permission",
"domain": "global",
"reason": "insufficientPermissions"
}
],
"status": "PERMISSION_DENIED"
}
}
My code and the rest of the plumbing (authorization code, access token etc) works because if I use this scope:
https://www.googleapis.com/auth/calendar
I am able to create the calendar.
So, anybody knows if https://www.googleapis.com/auth/calendar.app.created is deprecated or something? It is not listed in the V3 calendar documentation but I was hopping that it continue to work. I hate to ask the user for full permission to all calendars since I only want to create and access a secondary calendar and that scope supposed to do exactly what I need.
Thoughts?
As you can see under OAuth 2.0 Scopes for Google APIs, https://www.googleapis.com/auth/calendar.app.created is not a valid scope for Calendar API v3.
The documentation for Calendars:insert states that the only valid scope for creating a new calendar is https://www.googleapis.com/auth/calendar
You notice yourself that if you try to create a calendar with https://www.googleapis.com/auth/calendar.app.created - be it on OAuth2 playground or elsewhere - the request fails with a 403 error
CONCLUSION
Unfortunately https://www.googleapis.com/auth/calendar.app.created is not a valid scope, the acceptance of this scope on OAuth2 playground is a bug.
https://www.googleapis.com/auth/calendar.app.created might not seem as valid scope - but works for me.
Successfully could create new calendar with that scope, then manage events within it, and calendar itself appears on list of calendars in calendar.google.com
Can't really help much without knowing exactly the flow you have on creating and executing requests to google. Also keep in mind it worked for me with server side app. Using latest library provided by google for PHP.

Enable Google My Business API. Api calls failed

I tried to access GMB account list api with an access token having scope
https://www.googleapis.com/auth/business.manage
{
"error": {
"code": 403,
"message": "Google My Business API has not been used in project xxxxxx before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project=xxxxxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
"status": "PERMISSION_DENIED",
"details": [
{
"#type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developers console API activation",
"url": "https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project=xxxxx"
}
]
}
]
}
}
Whenever I tried to submit request from I got an email from GMB that
Your company already has a whitelisted project to access the Google My Business API. At this time, we are only whitelisting one project per business..
I checked, but there is no library with name Google My Business.
Current enabled Libraries are
Have you submitted the form to get access to the GMB API?
https://docs.google.com/forms/d/e/1FAIpQLSfC_FKSWzbSae_5rOpgwFeIUzXUF1JCQnlsZM_gC1I2UHjA3w/viewform
When you fill it, you have to precise the email addresses that Google will grant access to. Maybe someone else filled in for your company but didn't mention your email address.
In my case, when I received a confirmation email from the GMB team, I was able to see the GMB API when I searched for it in the console (see screenshot)
Then, all I had to do was to click to enable it.

Post message to MS Teams channel using Graph API

I'm trying to send a message to MS Teams using Graph API. I'm passing access token (AAD token) with it but still, it's giving me below error. I have given all the required permissions in Azure API permissions.
error:
{
"error": {
"code": "UnknownError",
"message": "",
"innerError": {
"request-id": "53a5aaff-3d39-42ce-bdc6-74d02a756be2",
"date": "2019-12-23T06:42:27"
}
}
}
API: https://graph.microsoft.com/beta/teams/{group-id-for-Teams}/channels/{channel-id}/messages/{message-id}/replies
Oh, if this is from a bot (not clear from the original question, but clarified in your later comment) then you don't need to use the Graph API at all - there's another way to send the message using the Bot Framework tools instead. You can do this either from within your bot, or from a different application altogether. I've got a few bots where the user schedules something, like when they want a message sent, where the bot saves it to a database and I have another application (mostly I use Azure Functions right now) to send the item on that schedule.
There are some important pieces of information you need to store though, which you can get any time the users sends your bot a message - it's the information you need to store so that you know how to connect directly to that user and that conversation. It's called Pro-active Messaging, and to see how to do this, see the answer I posted at Programmtically sending a message to a bot in Microsoft Teams
If you DON'T have any conversation history with the user ever (as in they have never spoken with your bot before, and you're trying to send the first message) then it gets more complicated... Let me know if that's the case though.
Sending message to a channel using graph api is a protected api and it needs access permission from Microsoft.
Access can be requested from Microsoft access reuqest form.
Once access is given from Microsoft add graph api in api permissions of your web app, and bingo you can get the response.

How to check credentials against botframework api

I'd like to check that my bot credentials (appId + appSecret) are ok to connect to https://api.botframework.com/bot/v1.0/messages.
I can't send a real message because i have no conversation running so I tried to post the following json message :
{ "type": "Ping"} but the response i got was
{
"error": {
"message": "Expression evaluation failed. Object reference not set to an instance of an object.",
"code": "ServiceError"
}
}
Is there any way to check if my access to the api is ok?
If you've registered your bot, you can visit the Bot Framework page, click on the My Bots menu, and select your registered bot. On your bot page, scroll down to the bottom left and there's a test box.
Also, you can use the emulator. It has a place in the upper right corner to replace the default credentials with your bot credentials. Then change the URL to where you have your bot deployed. Tip: remember to append 'api/messages' to the URL.
Download the BotFrameworkEmulator to test connectivity to your bot. It works on windows and OSX if you have mono installed. You can change the default settings that the emulator uses by typing '/settings' after running it. You will be prompted to enter your appId, appSecret and url endpoint for sending and receiving messages to/from your bot.
You can also use the directline rest api to initiate conversations and send messages to your bot

error : { "code": "MailboxNotEnabledForRESTAPI", "message": "REST API is not yet supported for this mailbox." }

I am new with the "outlook" api. i want to get all calender event,contact and mail from "outlook account".
but i does not get appropriate result with "outlook" account its working fine with "office365" account
i follow this documentation for get all required data
and i create application for getting data from here
but every time when i login with my outlook account it's give me an error like this.
Please help me out.
Thanks in advance...
Just ran into the same problem and found an explanation here. It appears we're going to have to wait for the accounts to be enabled unless you request a testing account as described in the link. Confirmed the same results when using the non-enabled account in the Outlook Oauth Sandbox.
From the first link
Because enabling mailboxes on Outlook.com for the Outlook REST API happens over a period of time, your existing Outlook.com account may take a while to get enabled. To test your app accessing data on Outlook.com mailboxes that have already been enabled, you can request a new, enabled Outlook.com developer preview account by emailing outlookdev#microsoft.com.
If your app accesses Outlook.com mailbox data, it should handle scenarios where the user's mailbox has not yet been enabled for the Outlook REST API. In such situations, when you make a REST request, you would get an error like the following:
HTTP error: 404
Error code: MailboxNotEnabledForRESTAPI or MailboxNotSupportedForRESTAPI
Error message: “REST API is not yet supported for this mailbox.
Step 1 :
Click the Below Link :
https://oauthplay.azurewebsites.net/?code=M657b8bab-e543-f849-134c-0a2f85179a67&state=17661047-2a14-4c90-9edd-0119f841b559
Step 2:
Step 3 :
Step 4 :
Step 5 :

Resources