Getting Messages from Inbox - javascript

I have a question about the Graph API.
I use Javascript for the API and make a little test website ,where you can log in ,look for new messages and write a new status.
My problem is that I can't get the messages or the thread.
FB.api('/me/inbox',function(response) { alert(response.id); } ); don't work.
Have somebody an example for getting the messages in the inbox??
Thanks

The /me/inbox request requires that you have the read_mailbox permission granted.
Once you've got that, the /me/inbox request will return an array of Thread's, which will look something like this;
{
"data": [
{
"id": "1126884978255",
"from": {
"name": "Someone's Name",
"id": "34723472"
},
"to": {
"data": [
{
"name": "Someone's Name",
"id": "34723472"
},
{
"name": "Matt Lunn",
"id": "560914724"
}
]
},
"message": "Testing the one-ness.",
"updated_time": "2012-01-31T12:13:00+0000",
"unread": 0,
"unseen": 0,
"comments": {
"data": [
{
"id": "1126884978255_6769",
"from": {
"name": "Someone's Name",
"id": "34723472"
},
"message": "£140!?",
"created_time": "2012-01-31T11:33:15+0000"
},
{
"id": "1126884978255_6771",
"from": {
"name": "Matt Lunn",
"id": "560914724"
},
"message": "^^ month in advance as well",
"created_time": "2012-01-31T11:33:26+0000"
}
]
},
"type": "thread"
}
],
"summary": {
"unseen_count": 0,
"unread_count": 21,
"updated_time": "2012-01-31T13:19:31+0000"
}
}
So depending which ID you're after, you'll have to do;
for (var i=0;i<response.data.length;i++) {
var thread = response.data[i];
for (var j=0;j<thread.comments.data.length;j++) {
var comment = thread.comments.data[j];
console.log(comment.message);
}
}
Hopefully you get the idea...

Related

Postman - How to count occurrences of a specific object in a JSON response

I am new to JSON and Postman. I believe I'm trying to do something very simple.
I have created a GET request which will get a JSON response like the one below.
In the example below I want to get the count of All "IsArchived" attributes in the response;
The number of those attributes will vary from response to response.
How can I do it? Thanks in advance
{
"Id": 1328,
"Name": "AAA Test",
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"FieldGroups": [
{
"Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
"Name": "General",
"FieldDefinitions": [
{
"Id": 1,
"DisplayName": "Product Name",
"IsArchived": false
},
{
"Id": 2,
"DisplayName": "Short Description",
"IsArchived": false
},
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
},
]
},
{
"Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
"Name": "Somethingelse",
"FieldDefinitions": [
{
"Id": 123,
"DisplayName": "Attribution",
"IsArchived": false
},
{
"Id": 1584,
"DisplayName": "FC1",
"IsArchived": false
},
{
"Id": 623,
"DisplayName": "Sizes",
"IsArchived": false,
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"Unit": "",
"Options": [
{
"Id": 1,
"Value": "XS"
},
{
"Id": 2,
"Value": "S"
},
{
"Id": 3,
"Value": "M"
}
]
}
]
}
],
"IsArchived": false
"Version": 1
}
It is a rather specific solution but I hope it helps. The description is added as comments:
// Convert the response body to a JSON object
var jsonData = pm.response.json()
// Create a count variable which will be increased by 1 everytime IsArchived occurs
var count = 0;
function countIsArchived() {
// Loop through the FieldGroupsArray
_.each(jsonData.FieldGroups, (fieldGroupsArray) => {
// Loop through the FieldDefinitionsArray
_.each(fieldGroupsArray.FieldDefinitions, (fieldDefinitionsArray) => {
// Check if IsArchived exists
if(fieldDefinitionsArray.IsArchived) {
// Increase count by 1
count++;
}
});
});
// IF you want it:
// Check if IsArchived exists on the top level of the JSON response and increase count
if(jsonData.IsArchived) {
count++;
}
// IF you want it:
// Create a Postman environment variable and assign the value of count to it
pm.environment.set("count", count);
}
Additional info:
The , after the following object is not needed. It invalidates the JSON:
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
}, <--

Tableau Web Data Connector error

I am creating a Tableau Web Data Connector as per the documentation HERE.
I am running the Simulator and have setup a HTML page (as per tutorial) which calls a Javascript file that runs the Tableau WDC script as below.
(function () {
var myConnector = tableau.makeConnector();
myConnector.init = function(initCallback) {
initCallback();
tableau.submit();
};
myConnector.getSchema = function (schemaCallback) {
var cols = [
{ id : "date_of_work", alias : "Date of Work", dataType: tableau.dataTypeEnum.date },
{ id : "supervisor", alias : "Supervisor", dataType: tableau.dataTypeEnum.string }
];
var tableInfo = {
id : "test",
alias : "test",
columns : cols
};
schemaCallback([tableInfo]);
};
myConnector.getData = function (table, doneCallback) {
$.getJSON("http://myDataCall.php", function(response) {
// ERROR HERE!
var resp = response.job.job_workflows; // Response
var parsedResp = JSON.parse(resp); // Parse the response
var tableData = []; // Temp array
// Iterate over the JSON object
for (var i = 0, len = resp.length; i < len; i++) {
tableData.push({
"date_of_work": parsedResp[i]['job_status'],
"supervisor": parsedResp[i]['job_workflow_1197927'],
});
}
table.appendRows(tableData);
doneCallback();
});
};
tableau.registerConnector(myConnector);
})();
When I run the script I get the error: The WDC reported an error:
Uncaught SyntaxError: Unexpected token o in JSON at position 1 stack:SyntaxError:
Unexpected token o in JSON at position 1 at JSON.parse () at Object.success
The (abbreviated) JSON that is being returned looks as follows:
{
"employee": {
"id": 23940,
},
"company": {
"id": 1059,
},
"job": {
"id": 13712707,
"job_status_logs": [{
"id": 17330391,
}],
"company": {
"id": 1059,
},
"created_by": {
"id": 23940,
},
"job_workflows": [{
"id": 1087689283,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251218,
"name": "Date of work",
"action": "datepicker",
"optional": 0,
"action_values": "",
"action_value_entered": "2017-10-12",
"nested_workflow_id": 0,
}, {
"id": 1087689284,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251219,
"name": "Supervisor",
"action": "list",
"optional": 0,
"action_values": "John Doe",
"action_value_entered": "John Doe",
"nested_workflow_id": 0,
}],
"job_fields": [{
"id": 50456098,
}],
"job_status_change_messages": [{
"id": 59957985}],
"job_assets":[]
}
}
I am trying to access the job.job_workflows.action_value_entered value but keep getting the error as above.
How can I fix this error?
There are a couple of issues here.
1) The JSON sent back from your server is invalid. Here is the valid version. I recommend using a site like https://jsonformatter.curiousconcept.com/ to validate your JSON.
{
"employee": {
"id": 23940
},
"company": {
"id": 1059
},
"job": {
"id": 13712707,
"job_status_logs": [{
"id": 17330391
}],
"company": {
"id": 1059
},
"created_by": {
"id": 23940
},
"job_workflows": [{
"id": 1087689283,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251218,
"name": "Date of work",
"action": "datepicker",
"optional": 0,
"action_values": "",
"action_value_entered": "2017-10-12",
"nested_workflow_id": 0
}, {
"id": 1087689284,
"job_id": 13712707,
"employee_id": null,
"template_workflow_id": 1251219,
"name": "Supervisor",
"action": "list",
"optional": 0,
"action_values": "John Doe",
"action_value_entered": "John Doe",
"nested_workflow_id": 0
}],
"job_fields": [{
"id": 50456098
}],
"job_status_change_messages": [{
"id": 59957985}],
"job_assets":[]
}
}
2) jQuery's getJson method returns an object, so you don't need to parse it. You can just use the resp variable directly like so:
var resp = response.job.job_workflows; // Response
var tableData = []; // Temp array
// Iterate over the JSON object
for (var i = 0, len = resp.length; i < len; i++) {
tableData.push({
"date_of_work": resp[i]['job_status'],
"supervisor": resp[i]['job_workflow_1197927'],
});
}
table.appendRows(tableData);
doneCallback();
Fixing those two issues should unblock you. However, you'll want to think through what data you are sending back. The current values you are sending back do not exist in the JSON (i.e. resp[i]['job_workflow_1197927']).
Instead, you could do something like this: resp[1].job_id, which would give you the job_id of each job_workflow.

elasticsearch.js bulk insert error

I am trying to insert/update data with the javascript elasticsearch client, but I am getting the error:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
}
],
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
},
"status": 400
}
This is the data that is being sent
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data":
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
]
})
It appears as this code generates the following request to ES:
-> POST http://docker.me:9200/_bulk
{
"index": {
"_index": "myindex",
"_type": "movie",
"_id": "1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data": {
"title": "Title 2",
"description": "This should be updated with this new.",
"score": 1,
"suggest_title": "Title 2",
"img": "http://url.to.image/img.jpeg",
"genres": [
"Comedy"
],
"release": "2015-01-07T23:00:00.000Z",
"language": "EN",
"provider": {
"id": "InstaFilmFlixify",
"url": "http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
What am I doing wrong? What is happening? Can this possibly be a bug in ES / the ES adapter.
Elasticsearch version 2.1
I haven't seen the "_data" parameter before. Where did you get the idea to use that?
Take a look at the docs for the js client.
Anyway, this should work for you:
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
}
},
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
]
})

Facebook Open Graph not returning all results for Books.Read?

When using the Facebook Open Graph explorer, I run the following, to get a list of my books read.
/706685881/books.reads?limit=25&offset=0
This returns only 5 results, even though I have more books read than that, and I've explicitly requested up to 25 results. Following the paging link to the (theoretical) more results, shows a page with no more results.
here's my output of the query:
{
"data": [
{
"data": {
"book": {
"id": "452418224806835",
"url": "https://www.facebook.com/pages/Fantastic-Voyage/452418224806835",
"type": "books.book",
"title": "Fantastic Voyage"
}
},
"id": "10151871872740882"
},
{
"data": {
"book": {
"id": "106151566083577",
"url": "https://www.facebook.com/pages/The-Andromeda-Strain/106151566083577",
"type": "books.book",
"title": "The Andromeda Strain"
}
},
"id": "10151870390740882"
},
{
"data": {
"progress": [
{
"timestamp": 1388668020,
"percent_complete": 0
},
{
"timestamp": 1392949225,
"percent_complete": 100
}
],
"book": {
"id": "10150233066083545",
"url": "http://www.goodreads.com/book/show/10664113-a-dance-with-dragons",
"type": "good_reads:book",
"title": "A Dance with Dragons (A Song of Ice and Fire, #5)"
}
},
"id": "10151867499170882"
},
{
"data": {
"progress": [
{
"timestamp": 1391350736,
"percent_complete": 0
},
{
"timestamp": 1392569208,
"percent_complete": 100
}
],
"book": {
"id": "1390736374489290",
"url": "http://www.goodreads.com/book/show/18405477-city-of-the-sun",
"type": "good_reads:book",
"title": "City of the Sun"
}
},
"id": "10151860185710882"
},
{
"data": {
"book": {
"id": "526531387368738",
"url": "http://apps.facebook.com/bookscout/?currworkid=9000000000133742",
"type": "books.book",
"title": "Gates of Fire"
}
},
"id": "10151226827780882"
}
],
"paging": {
"next": "https://graph.facebook.com/706685881/books.reads?fields=data&limit=25&offset=25&__after_id=10151226827780882"
}
}
Anybody seeing what I am doing wrong?

I update the same video ID, but the youtube-api v3 still show "Video not found"

I used google js api Link1and youtube v3 api Link2
I want use js to update my video, the js code for listing videos:
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
//id: ['uY3InyzU79U','nOoHrAR50f8'],
myRating:'like',
maxResults: 9
};
if (pageToken) {
requestOptions.pageToken = pageToken;
pToken=pageToken;
}
var request = gapi.client.youtube.videos.list(requestOptions);
request.execute(somefunction());
The json response:
[
{
"id": "gapiRpc",
"result": {
"kind": "youtube#videoListResponse",
"etag": "\"Wes3wj_Yozi4a9GfHnpc8qG73KI/8qQBat1N6baCDG6C3Eed_XV_sZo\"",
"pageInfo": {
"totalResults": 3,
"resultsPerPage": 9
},
"items": [
{
"id": "nOoHrAR50f8",
"kind": "youtube#video",
"etag": "\"Wes3wj_Yozi4a9GfHnpc8qG73KI/f7JZaoHeZG-CDYzXVRw2Q11Lins\"",
"snippet": {
"publishedAt": "2013-05-30T05:37:50.000Z",
"channelId": "UCx0RClYCMhtsOXRlSyRcuRA",
"title": "Webcam video from May 30, 2013 3:36 PM",
"description": "",
"thumbnails": {
"default": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/default.jpg"
},
"medium": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/mqdefault.jpg"
},
"high": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/hqdefault.jpg"
},
"standard": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/sddefault.jpg"
}
},
"channelTitle": "foretribe1",
"tags": [
"test",
"webcam"
],
"categoryId": "22"
}
}
]
}
}
]
the update video js code:
var requestOptions = {
part:'snippet',
id: String(id),
snippet:{ title: "ok update"}
};
var request = gapi.client.youtube.videos.update(requestOptions);
request.execute(nothing());
the request json:
[{"jsonrpc":"2.0",
"id":"gapiRpc",
"method":"youtube.videos.update",
"params": {
"part":"snippet",
"id":"nOoHrAR50f8",
"snippet":{"title":"ok update"}
},
"apiVersion":"v3"
}]
you can see here the id is the same as the list request json, but I still get the error respond:
[
{
"error": {
"code": 500,
"message": "Video not found",
"data": [
{
"message": "Video not found"
}
]
},
"id": "gapiRpc"
}
]
I also test on Link2, but also get 500 error.
You need wrap your updates in a 'resource' attribute:
gapi.client.youtube.videos.update({
fields: "snippet",
part: "snippet",
resource: {
id: "vid_id",
snippet: {
description: "updating vid",
}
}
});
Remember to set other attributes in the snippet (like categoryId, tags, etc.); otherwise, you'll get another error or they'll be unset.
Probably the best way to do this is to first retrieve the video snippet using gapi.client.youtube.videos.list and alter the snippet, then call gapi.client.youtube.videos.update passing in the altered snippet in the resource attrbute.

Categories

Resources