GMail API Users.threads.list Missing "Messages" Field - javascript

I'm able to use GMail's Users.threads.list API call to retrieve a list of the threads. I'd like to also grab the labels belonging to each message in the thread.
On the official documentation / live example for this method, there is an area called fields where the user can specify the optional fields they wish to pull in. They even provide a little UI tool call 'fields editor' to help you select and properly format the fields for inclusion in the REST query:
Which results in the following, valid, generated fields parameter values:
nextPageToken,resultSizeEstimate,threads(historyId,id,messages/labelIds)
The final request looks like this:
GET https://www.googleapis.com/gmail/v1/users/me/threads?includeSpamTrash=true&fields=nextPageToken%2CresultSizeEstimate%2Cthreads&key={YOUR_API_KEY}
After authenticating with OAuth2, I get back a list of threads[], but none of them include the expected messages[] array nested within them! I know the field mask is valid because GMail API errors out when you request a field the method doesn't support with a standard HTTP 400 and a pretty printed error message. But in this case, all I get back is an object that contains an array labeled "threads", where each thread object only has the following fields:
id
snippet
historyId
But no messages array. Stranger yet, even when I remove the fields custom filter parameter, I still get back these same results. Am I missing something obvious here?

You have to list the id of the threads first, and then make a separate request to get the thread and fields in the messages you want. The API Explorer allows you to select additional fields when listing for some reason.
1. List ids
Request
GET https://www.googleapis.com/gmail/v1/users/me/threads?access_token={access_token}
Response
{
"threads": [
{
"id": "1570f8c16a1084de",
"snippet": "Foo bar...",
"historyId": "1234"
}, ...
],
"resultSizeEstimate": 100
}
2. Get the thread
Request
GET https://www.googleapis.com/gmail/v1/users/me/threads/1570f8c16a1084de?access_token={access_token}
Response
{
"id": "1570f8c16a1084de",
"historyId": "1234",
"messages": [
{
"id": "1570f8c16a1084de",
"threadId": "1570f8c16a1084de",
"labelIds": [
"INBOX",
"IMPORTANT",
"CATEGORY_FORUMS"
], ...
}
}
If you don't want to list threads and then get them all separately, you could use batch requests to bring it down to 2 requests instead.

Related

Good data structure for storing collection of items for most effiecient query DynamoDB

I recently ran into a problem where I had to store a collection of data inside an attribute. There is restaurantDB table. The table has an attribute(not required) named "groups" that manages the groups created by the restaurant. These groups have several permissions associated with them. A natural way of thinking is that the groups attribute can be of type list that stores a map(key-value pairs) of the data like group_name, group_id, and permission(that is a list of all permisssions)
table:{
//other fields
id:
groups: L: M: {group_name:S,group_id:S,permission:L :S}
}
e.g.
"groups": [{
"name": "OWNER",
"permission": ["READ", "WRITE"]
},
{
"name": "MANAGER",
"permission": ["READ"],
}
],
This works fine for creation and appending arbitrary number of users using dynamoDB aws-sdk
update with UpdateExpression: 'SET #groups:=list_append(#groups,:newgroup)'
however if i have to do a patch request to modify a permission for a group, say, MANAGER,
How can i retrieve the Map object inside the list with key group_name:"MANAGER" without fetching the whole array of Groups.
I don't want to patch it by fetching the whole list because I will first have to query to get hold of the groups attribute, then I'll have to iterate through whole of the array to findgroup_name:"MANAGER". Once I get hold of it I'll then modify the array then put the whole list back with an update.
The group names are gauranteed to be unique(however the client also wants a unique group_id) so I thought of a data structure something like
table:{
//other fields
id:
groups: M:{S:L}
}
e.g.
"groups":{
"OWNER":["READ","WRITE"],
"MANAGER":["READ"]
}
Though now I cannot enter arbitrary number of data to create a new group(as in POST request) but I can do it one at a time. Also PATCH request now work fine as
UpdateExpression:
"set #groups.#groupName = :newPermission",
ExpressionAttributeNames: {
"#groups": "groups",
"#groupName": `${groupName}`, //groupName taken from request Body
},
ExpressionAttributeValues: {
":newPermission": permission, //permission taken from request body
},
I wanted to know if there was a better way to retrieve list types. The documentation syas we can retrive list items with Indices however I don't know the index in advance.

How to pass a value from slack channel to `Options Load URL` in Slack App

I have a slack app implemented in node.js where I am dynamically displaying a drop-down menu from an Options Load URL to a channel on the Slack group.
The drop down is getting displayed correctly based on the options JSON that I am returning form the external URL,
but now the problem is that I need to have separate items in the drop-down menu based on what the user has entered on the slack channel.
For example:
If the user says: give me choices for option 1: then the value 1 should be passed to the Options Load URL and the code that I have implemented at that URL will reply with the appropriate JSON based on the input value 1.
Next, when the User says give me choices for option 2: then the value 2 should be passed to the Options Load URL and the code implemented there will reply the options based on the value 2 that it receives.
The code at the Options Load URL is already implemented. The code for extracting the number 1 or 2 from the user message is also implemented.
The values 1 or 2 ... etc. are not constant or fixed. These can by random and the API at Options Load URL will be able to correctly handle these values.
I just need to figure out a way to send these values to the Options Load URL somehow.
Is it possible to do this somehow in Slack?
Use the name property in your request to pass a custom value to the part of your app that handles the options requests from Slack ("Options Load URL"). I use it usually to select which predefined options list to return, but you can also use it to dynamically create a new option list based on the value.
For reference here is the example Slack request for creating a dynamic menu (from the offical documentation), where you can see the name property under action. In this example it has the value "bugs_list":
{
"text": "What's bugging you?",
"response_type": "in_channel",
"attachments": [
{
"fallback": "Upgrade your Slack client to use messages like these.",
"color": "3AA3E3",
"attachment_type": "default",
"callback_id": "select_remote_1234",
"actions": [
{
"name": "bugs_list",
"text": "Which random bug do you want to resolve?",
"type": "select",
"data_source": "external",
"min_query_length": 3,
}
]
}
]
}
And here is what your Options Load URL will receive. Notice the name parameter.
{
"name": "bugs_list",
"value": "bot",
"callback_id": "select_remote_1234",
"team": {
"id": "T012AB0A1",
"domain": "pocket-calculator"
},
"channel": {
"id": "C012AB3CD",
"name": "general"
},
"user": {
"id": "U012A1BCJ",
"name": "bugcatcher"
},
"action_ts": "1481670445.010908",
"message_ts": "1481670439.000007",
"attachment_id": "1",
"token": "verification_token_string"
}
The name parameter is a common parameter in a HTTP request, so you can put pretty much everything in there, even data structures, as long as they are encoded as strings. Also see my other answer that talks about the limits of passing data in Slack parameters.

How to get all google reviews using business api

I need all google reviews for particular location but I am unable to use business api from google. Here is url for get request
https://mybusiness.googleapis.com/v3/accounts/account_name/locations/location_name/reviews
Now my question is what is the value for param account_name and location_name
How can I get that.
Please answer with sample location example
I think first of all you need to white list your google my business api for whatever project you are working on in your project as its private api. Google my business api will work on the locations associated with your account so make sure you verified the LOCATIONS from any account you know. Then you can try out the api call you mentioned in OAuthplayground.
Follow steps mentioned in below documentation URL to set it up:
https://developers.google.com/my-business/content/prereqs
After the setup and etc you will automatically understand the account id and location id.
Also few more urls you can go to understand it better.
https://console.developers.google.com (here you will setup your project)
https://business.google.com/manage (here you will add/can see the locations - for which you need reviews)
https://developers.google.com/my-business/content/basic-setup (Steps after completing the prereq)
https://developers.google.com/oauthplayground (You will test the my
business api here after approval)
When you make a request to https://mybusiness.googleapis.com/v3/accounts it gives you a list of accounts. On those accounts they have a field called name. That field is accounts/account_name.
{
"state": {
"status": "UNVERIFIED"
},
"type": "PERSONAL",
"name": "accounts/1337",
"accountName": "example"
}
When you make a request to https://mybusiness.googleapis.com/v3/accounts/account_name/locations it gives you a list of locations. On those locations they have a field called name. That field is accounts/account_name/locations/location_name.
{
"locations": [
{
"languageCode": "en",
"openInfo": {
"status": "OPEN",
"canReopen": true
},
"name": "accounts/1337/locations/13161337",
...
}

Getting like and comment counts per post of facebook page by graph api version 2

I have been getting like and comment counts per post of facebook page/group feed call by graph api separately using FQL but as version 2 of graph api released fql no longer working to serve purpose.
So i have to find new ways to get comment and like counts per post of page feed display. I will make a separate call to get comment and like counts per post of the fb page as it may not be possible to get things in same page feed call(or it is?).
So, searching through google, i found following way using graph api call -
..page_id/feed?fields=likes.limit(1).summary(true){id},comments.limit(1).summary(true)&limit=10
Is this the best and error free way?? Also besides id and summary fields i also get created_time, paging, likes data by the above call which is unexpected and redundant, how do i exclude these additional fields?
So please any FB employee show me light on what is the best way to retrieve like and comment count per post of page/group feed using graph api version 2.
If you want to retrieve Likes and comments count of a post on FB you can achieve this by using Id of the post Like this
..Your_Post_ID?fields=likes.limit(0).summary(true),comments.limit(0).summary(true)
the result will contain
Total numbers of likes of the post, total numbers of comments of the post, post ID and post created time.
The result will be like this
{
"likes": {
"data": [
],
"summary": {
"total_count": 550
}
},
"comments": {
"data": [
],
"summary": {
"order": "chronological",
"total_count": 858
}
},
"created_time": "2014-10-12T05:38:48+0000",
"id": "Your_Post_ID"
}

Facebook Like Button - Is It Checked?

I'm having trouble finding good documentation about how to use javascript in order to find out if a like button is clicked or not. I can't use an on-click event because the button might already be checked when a user comes onto my page but that seems to be all I can find regarding tracking likes externally (not counting other methods that are no longer supported). Anyone have any experience with such issues? Thanks.
It depends on the Like button. If it's for a Facebook object that has an ID, you can query the like table (but you'll need user_likes permission)
If it's for a URL, it's not really possible: facebook graph api determine if user likes url [stackoverflow]
It it's for a built-in like you could use a cookie to remember the click, or save the click in your own database.
Just to expand on Gil's answer. For built-in likes you can use a batch request consisting of two request:
1) Try to like the object in question
2) If there was no error, delete the like connection
Example batch:
[{"method":"POST", "relative_url":"me/og.likes", "body":"object=<SOME_URL>", "name":"like-attempt", "omit_response_on_success": false},
{"method":"DELETE", "relative_url":"{result=like-attempt:$.id}"}]
If an object was already liked, batch response would be:
[
{
"code": 400,
"headers": [
...
{
"name": "WWW-Authenticate",
"value": "OAuth \"Facebook Platform\" \"invalid_request\" \"(#3501) User is already associated to the object object on a unique action type Like. Original Action ID: 143539809123515\""
}
],
"body": "{\n \"error\": {\n \"message\": \"(#3501) User is already associated to the object object on a unique action type Like. Original Action ID: 143539809123515\",\n \"type\": \"OAuthException\",\n \"code\": 3501\n }\n}"
},
null
]
So to check if object was liked, JSON.parse the first response body and look for an error with error code 3501. Keep in mind that the actual error code is not documented anywhere so it could change, although I don't think it is likely.

Categories