Impossible to get messages details in a list request with Gmail API - ruby-on-rails

I am using a ruby on rails app which connects to the Gmail API. When I make a listrequest to get all the messages of one mailbox, I only get back an idand a threadId property for each message.
I tried to follow Gmail API Doc using the fields parameters to get other properties (title, date...). It doesn't work, whether I use the google-api-client gem in my app, or by doing a direct GET request.
Adding any other parameters to the request ends with a failure. Here is the url that works :
https://www.googleapis.com/gmail/v1/users/me/messages?fields=messages(id,threadId)
Am I forced to make one call per message or using batch requests to get relevant datas ? It seems a little heavy...

You first need to list messages like you've done, and then get each message in a separate request.
Request 1
GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1&access_token={ACCESS_TOKEN}
Response 1
{
"messages": [
{
"id": "15fd9f0fe242f975",
"threadId": "15fd9f0fe242f975"
}
],
"nextPageToken": "11889180580605610074",
"resultSizeEstimate": 2
}
Request 2
GET https://www.googleapis.com/gmail/v1/users/me/messages/15fd9f0fe242f975?access_token={ACCESS_TOKEN}
Response 2
{
"id": "15fd9f0fe242f975",
"threadId": "15fd9f0fe242f975",
"labelIds": [
"IMPORTANT",
"CATEGORY_UPDATES",
"INBOX"
],
"snippet": "Tasks tracked last week...",
"historyId": "966691",
...
}
It's also possible to get the total amount of request down from 1 + n of messages to 2 by using batch requests.

Related

Using RSC To Access Chat Messages with Microsoft Graph

I am building a Teams chat-bot that looks at the history of messages in the current chat/channel whilst in conversation with the user.
My bot has been granted all the RSC (Resource-Specific Content) Permissions it needs (see image below)
Here is the relevant parts of the manifest:
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.11/MicrosoftTeams.schema.json",
"version": "1.0.0",
"manifestVersion": "1.11",
"id": "bd33f8b1-b593-433c-926e-44a27c1bd94a",
...
"permissions": [
"identity",
"messageTeamMembers"
],
...
"bots": [
{
"botId": "e6d93739-a8ab-412d-a4f6-b6f514a3451a",
"scopes": [
"team",
"personal",
"groupchat"
],
"isNotificationOnly": false,
"supportsFiles": true
}
],
"validDomains": [],
"webApplicationInfo": {
"id": "e6d93739-a8ab-412d-a4f6-b6f514a3451a",
"resource": "https://RscBasedStoreApp",
"applicationPermissions": [
"TeamSettings.Read.Group",
"ChannelMessage.Read.Group",
"TeamSettings.Edit.Group",
"ChannelSettings.ReadWrite.Group",
"Channel.Create.Group",
"Channel.Delete.Group",
"TeamsApp.Read.Group",
"TeamsTab.Read.Group",
"TeamsTab.Create.Group",
"TeamsTab.ReadWrite.Group",
"TeamsTab.Delete.Group",
"Member.Read.Group",
"Owner.Read.Group",
"ChatSettings.Read.Chat",
"ChatSettings.ReadWrite.Chat",
"ChatMessage.Read.Chat",
"ChatMember.Read.Chat",
"Chat.Manage.Chat",
"TeamsTab.Read.Chat",
"TeamsTab.Create.Chat",
"TeamsTab.Delete.Chat",
"TeamsTab.ReadWrite.Chat",
"TeamsAppInstallation.Read.Chat",
"OnlineMeeting.ReadBasic.Chat",
"Calls.AccessMedia.Chat",
"Calls.JoinGroupCalls.Chat",
"TeamsActivity.Send.Chat"
]
}
}
Note: the bot has permission to read messages in chats and channels. Specifically, my problem affects chats and not channels (which I can get messages from fine).
In order to do this, I get a JWT token for the bot account, accessing the Graph API like so:
GraphServiceClient<?> gsc = GraphServiceClient.builder()
.authenticationProvider(u -> mac.getToken())
.buildClient();
Next, I am using the Graph API to pull back these messages. For messages in channels I can do:
gsc.teams("some group id")
.channels("team id")
.messages()
.buildRequest(Collections.emptyList()).get()));
This works fine.
For chats, I am doing something like:
gsc.chats("29:13qY8hmfkJinH9-v7rYKjCNFHYFJXKbjqR-NyzyKzL694npelHJoq5HrVtqJLRYo79OYeHGQq-bhtJM5N-yKXyQ")
.messages()
.buildRequest().get()));
However, this time I get an error from the Graph API:
[Some information was truncated for brevity, enable debug logging for
more details] com.microsoft.graph.http.GraphServiceException: Error
code: Forbidden Error message: Invoked API requires Protected API
access in application-only context when not using Resource Specific
Consent. Visit
https://learn.microsoft.com/en-us/graph/teams-protected-apis for more
details.
GET
https://graph.microsoft.com/v1.0/chats/29:13qY8hmfkJinH9-v7rYKjCNFHYFJXKbjqR-NyzyKzL694npelHJoq5HrVtqJLRYo79OYeHGQq-bhtJM5N-yKXyQ/messages
SdkVersion : graph-java/v5.6.0
I am at a loss to explain why querying channels works fine but querying chats fails.
Any help gratefully appreciated!
This is a protected API and in order to use it you will first need to make a formal request to Microsoft Graph, asking for permissions to use the API without any user interaction
Here is the list of protected APIs. You need to fill this form to get the required permissions.
To request access to these protected APIs, complete the following
request form. We review access requests every Wednesday and deploy
approvals every Friday, except during major holiday weeks in the U.S.
Submissions during those weeks will be processed the following
non-holiday week.
The other option would be to use delegated flow.

Cumulocity SmartREST GET Template using MQTT

I am trying to receive a response using a custom GET template implemented on the Cumulocity UI however I'm having some trouble. I have defined my message template like so:Message Template and my response template like so: Response template.
At the moment when I publish to topic 's/uc/template1' (where template1 is my X-ID) with the payload of '999, 12345'(where 12345 is my External ID), no response is received and no error is found. The speeds are known to exist in the object because when I request a GET command to {{url}}/inventory/managedObjects this responds with:
"speed": {
"1": {
"unit": "m/min",
"value": 1
},
"2": {
"unit": "m/min",
"value": 1
}
I've also followed this post where #TyrManuZ suggests to add c8y_Global: {} to the application. I have done this through a PUT request to {{url}}/inventory/managedObjects/{{deviceId}} on Postman and can verify that c8y_Global: {} exists there.
Is there something I am doing wrong, and if so what is the correct way? Any help would be greatly appreciated!

microsoft-graph message ID

I call my mailbox using REST calls to MSGraph:
GET https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages
As expected I receive a Json with my mails.
To play with a specific mail (move, delete,forward, ...) I saw that the API should be called like
POST me/messages/{id}/forward
I can't target the message if I provide the value of the id field that I get from the JSon. Where to find the correct {id} that I should use?
my result is like the example at https://developer.microsoft.com/en-us/graph/graph-explorer see id: field when calling GET my mails.
Thanks
The id from JSON is OK. what you missing is you have not set the post conent for move/forward.
If you use the forward API, you should set the post content like below:
{
"Comment": "FYI",
"ToRecipients": [
{
"EmailAddress": {
"Address": "XXX#XXX.onmicrosoft.com"
}
}
]
}
This is the worked forward url for me(you can implement the move/delete like this too):
https://graph.microsoft.com/v1.0/me/messages/AAMkAGNjZTcyZDJmLTkzMjYtNGQwNi05Y2UxLTc5NDk0NjEwNjJjMABGAAAAAAAUYpVLVYiKTINMO3MZR0H5BwB3NF3PTyl5QIQd59edwRO9AAAAAAEMAAB3NF3PTyl5QIQd59edwRO9AAApBI9fAAA=/forward
For information please see:
https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/mail-rest-operations#ForwardDirectly

Getting status for playlist from Youtube API

I'm building an app that based on user's choice connects to youtube api and get's list of videos of certain playlist.
I would like to make it safe to use and if the status is not 200, make it impossible for the next screen to show.
The JSON result from my request does not return any information on the status other than privacy status (similarly, if I get data for a video not a playlist I get detailed information) :
var urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=500&playlistId=\(playlistId)&key=XXXXX"
"status":{
"privacyStatus":"public"
}
How do I verify that the request for the playlist is valid?
Thanks!
I have not found a reliable way to test the YT api status, so I generally will look for results from the call that should be there. And if they are not there (i.e. empty or '0' ) , then it is normally a failed call.
So [totalResults] and/or [title] as example.
There are two parts here.
You need to determine if the actual HTTP request was successful or not (that is the request to https://googleapis/youtube/v3/[method]?[parameters])
You have a specific requirement where you need to handle cases where there is no data. The response from the API itself will not explicitly provide this to you, instead you need to define what failure means.
With this in mind, you could implement two levels of checks:
Check 1 - HTTP status code
The first level would check the HTTP status code (200 = OK, 400 = Bad Request, etc.).
If your API request returns a status of 200, you can move on to the next level of checks (see Check 2). However, if your API request returns a status of 400, then the request has failed and no further checks are required (or necessary).
Note: If you do get a 400 status, there is some JSON in the response which contains detailed information regarding the error.
{
"error": {
"errors": [
{
"domain": "youtube.parameter",
"reason": "missingRequiredParameter",
"message": "No filter selected. Expected one of: id, playlistId",
"locationType": "parameter",
"location": ""
}
],
"code": 400,
"message": "No filter selected. Expected one of: id, playlistId"
}
}
Check 2 - Data
The second level would check to see if any results have been returned (as it seems possible to have a HTTP status code of 200 and no results).
This has nothing to do with the status of the actual HTTP request itself. Instead you have to check the response to see if the data you application requires is present or not.
With the example of a 200 response below, the items array is empty. So you could say that even though the request was successful, this is a failure condition and not show your next screen.
{
"kind": "youtube#playlistItemListResponse",
"etag": "\"[etag would appear here]\"",
"nextPageToken": "CAAQAA",
"pageInfo": {
"totalResults": 199,
"resultsPerPage": 0
},
"items": [
]
}
Note: The above is an example where the results per page was purposely set to 0.

Look a message's text from events api response

I'm using slacks events API and have setup a subscription to the reactions_added event. Now when a reaction is added to a message, slack will send me a post body with all the details of the dispatched event as described here.
The problem I'm having is that I want to get the details, specifically the text of the message that my users have reacted to so I can parse/store etc that specific message. I assumed the message would return with some type of UUID that I could then respond to the callback and get the text, however I'm find it difficult to get the specific message.
The only endpoint I see available is the channels.history, which doesn't seem to give me the granularity I'm looking for.
So the tl;dr is: How do I look up a via slacks API, a messages text sent from the events API? Give the information I have the event_ts, channel and message ts I thought would be enough. I'm using the ruby slack-api gem FWIW.
You can indeed use the method channels.history (https://api.slack.com/methods/channels.history) to retrieve message from a public channel . The reaction_added dispatched event includes the channel ID and timestamp of the original message (in the item) and the combination of channelId + timestamp should be unique.
Be careful that you use the correct timestamp though. You need to use item.ts not event_ts
Full example dispatched event from the docs:
{
"token": "z26uFbvR1xHJEdHE1OQiO6t8",
"team_id": "T061EG9RZ",
"api_app_id": "A0FFV41KK",
"event": {
"type": "reaction_added",
"user": "U061F1EUR",
"item": {
"type": "message",
"channel": "C061EG9SL",
"ts": "1464196127.000002"
},
"reaction": "slightly_smiling_face"
},
"event_ts": "1465244570.336841",
"type": "event_callback",
"authed_users": [
"U061F7AUR"
]}
So calling channels.history with these values set should work:
latest = item.ts value
oldest = item.ts value
inclusive = 1
channel = item.channel value
If you want to get messages from a private channel you need to use groups.history.
https://api.slack.com/methods/channels.history

Resources