How to pull attachments from Google Glass item? - node.js

I created NodeJS server that communicate with Google Glass, I want to know how to pull attachment from item, below you can see the item with attachments:
Note: in my project I already have:
*Send item item to glass(contact, card, location, etc..)
*Subscription to timeline collection
*Contact with callback to let Glass user share content - for more info Visit How to add another option to the share functionality of Google Glass?
Do I need to use the selfLink to pull the attachment? if yes then how I can execute HTTP request for the selfLink while including the token?

The selfLink refers to the URL of the timelineItem itself. You want to look at the attachments attribute of the object. It might look something like this:
{ "kind": "mirror#timelineItem",
"id": "da61598c-2890-4852-2123-031011dfa004",
...
"attachments": [
"id": ...
"contentType": "image/jpeg".
"contentUrl": "https://www.googleapis.com/mirror/v1/timeline/da61598c-2890-4852-2123-031011dfa004/attachments/ps:605507433604363824",
"isProcessingContent": false
]
}
You should check to make sure isProcessingContent is false before you try to fetch it, otherwise the fetch will fail. This is usually pretty quick for images, but can take longer for video.
See more at https://developers.google.com/glass/v1/reference/timeline/attachments
To fetch it, you can issue an HTTPS request to that URL with an Authorization header with a value of Bearer auth_token (replacing auth_token with the actual value of the auth token).
To make the request itself, you'll probably want to use the http.request() method. So something like this (untested) might work:
var item = {the item you got sent above};
var attachment = item.attachments[0];
if( !attachment.isProcessingContent ){
var contentUrl = url.parse( attachment.contentUrl );
var options = {
"hostname": contentUrl.hostname,
"path": contentUrl.path,
"headers": {
"Authorization": 'Bearer '+authToken;
}
}
https.request( options, function(res){
// Get the image from the res object
});
}
See the documentation for URL.parse and HTTPS.request for details.

Related

Modiy axios request 'data' field into 'body'

I'm trying to create a POST request using axios to a Notification API. So this Notification API have been developed before thus I cannot change anything related to it.
It accepts the request more or less like below.
I need to have the request body in the field called body as shown below. However, axios sends the request body in the field called data rather than body
Expected request
{
"head": {
...
},
"body":{
"publicUserId":"abcd",
"merchantId":"123888",
}
},
}
My request composed automatically by axios:
Axios request
{
"head": {
...
},
"data":{
"publicUserId":"abcd",
"merchantId":"123888",
}
},
}
I did try searching for how to alter the request body field name to body instead of data but I'm pretty sure I haven't found the solution. Maybe if anyone here has some workarounds, I'd be glad to try.
Should any details need to be provided, please tell me. Thank you.

getting 403 error while sending file to githib via REST using nodejs

I want to send multiple files to Github repository via nodejs. Tried several approaches and end up using node-rest-client module. Tried below code send a sample file to repository called 'metadata'. But after post I am getting error message "Request forbidden by administrative rules. Please make sure your request has a User-Agent header"...please let me know if anyone faced this error before and get rid of it.
convertval = "somedata";
var dataObj = {
"message": "my commit message",
"committer": {
"name": "Scott Chacon",
"email": "ravindra.devagiri#gmail.com"
},
"content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}
debugger;
var Client = require('node-rest-client').Client;
var client = new Client()
var args = {
data: dataObj,
headers: { 'Content-Type': 'application/json' },
};
client.post("https://api.github.com/repos/metadata/contents", args, function (data, response) {
console.log("file send: True : " + data);
});
According to the REST API:
All API requests MUST include a valid User-Agent header. Requests with
no User-Agent header will be rejected.
First of all, you need to define 'User-Agent' with value 'request' in your request header. Refer to this link.
Second, endpoint you are trying to call might require authentication. Generate a personal token from here, add that token in your request header, 'Authorization': 'token '.
If you're using Git extensively in your code, I suggest you to use this - Nodegit.
Edit:
I don't think sending multiple files in a single request is possible in 'Contents' endpoints group (link).
You can checkout Git Data API (as discussed here).

Cannot fetch internet message headers for some emails

I'm using the Microsoft Graph Client for Node.js to fetch email messages. I'm only interested in the headers. The code is working, but recently I got some errors in fetching internetMessageHeaders via the API.
For some messages, the API doesn't return any data for this field (and the code wasn't checking for this, hence the errors).
I tried to manually run the queries using POSTMAN. Using ?$select=internetMessageHeaders as a query param to only fetch the headers. If I remove the query param, I can fetch the message normally (body, etc.).
I haven't been able to find anywhere in the docs why this would happen.
Any ideas on what it's happening?
Edit: Here is the node.js code I am using
const MicrosoftGraphClient = require("#microsoft/microsoft-graph-client").Client
async function fetchEmailMessageHeaders(id, credentials) {
let client = MicrosoftGraphClient.init({
authProvider: callback => callback(null, credentials.access_token)
});
let req = client.api(id).select("internetMessageHeaders");
let message = await req.get();
return message.internetMessageHeaders;
}
Sample output (value of message var):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('156751349d3cc68b')/messages(internetMessageHeaders)/$entity",
"#odata.etag": "W/\"CQAAABYAAABcr9US8aH2RIGaOGQZwDg3AAKyfdpk\"",
"id": "AQMkADAwATM0MDAAMS1hZWIxLThjZmYALTAwAi0wMAoARgAAA3wcQrEUgfhHoZ0BD2jmyXYHAFyv1RLxofZEgZo4ZBnAODcAAAIBDAAAAFyv1RLxofZEgZo4ZBnAODcAArJaPEcAAAA=",
"internetMessageHeaders": [
{
"name": "Received",
"value": "from BY2NAM01HT225.eop-nam01.prod.protection.outlook.com (2603:10a6:803:118::39) by VI1PR0301MB2221.eurprd03.prod.outlook.com with HTTPS via VE1PR03CA0050.EURPRD03.PROD.OUTLOOK.COM; Wed, 10 Apr 2019 11:41:55 +0000"
}
...
]
}
The problem is that for some emails, the message does not contain internetMessageHeaders.
I am using an inbox subscription to receive events for new email messages from Outlook. When an event is received I use the API to retrieve the message and extract headers from it.
API reference here
Edit2:
Here is an example JSON output of an event that fails to also retrieve headers:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('53e07bf4-bb6a-4a82-a724-37dadfb1cf11')/messages(internetMessageHeaders)/$entity",
"#odata.etag": "W/\"CQAAABQAAADdeU+v2VzbRpZMSJGral7kAAXJkA==\"",
"id": "AAMkADhmMmVmM2NjLWFhNGMtNDBlYy04NzBkLTg5MmU2OWI0ODU4MgBGAAAAAAALg6E-e6CxRLEbZfrhFaDBBwAzAAMgKF1iTp242t34rFy5AAAAAAAOAAB5l_O62bUUTqTLk6KYrl4sAAGrJcjfAAA="
}
It seems internetMessageHeaders are not set when an email is replied to using outlook.office365.com. I'm also not able to see message headers when inspecting the message using Outlook on Mac.

How to get body content from request in mobile service in Azure

I crate a mobile service and also a custom api for that in Azure. I use fiddler to send request and do some basic and simple testing.
At Azure side, I create a custom api for my mobile service. Let say the name is ExampleCustomApi. And in the code I have
exports.put = function(request, response) {
var tags = request.parameters.tags;
...
...
}
At fiddler side, I set http method to "PUT" and the url to my custom api.
Then I set a request body to { "tags": "tag1" }.
When I execute the request in fiddler, I receive 500 back. The log in Azure's mobile service says that parameters is undefined.
My question, then, is how to get the request body at the server side. I look at the document of request object, it seems to me that parameters is the one I should use, but it doesn't work.
request is an object in express.js library.
This is the documentation from MSDN http://msdn.microsoft.com/library/azure/jj554218.aspx
the documentation from express.js http://expressjs.com/api.html#req.body
And I can use request.body to get the body content.
After many attempts we cracked getting the content from a weburl. Hope this helps :) Jsonbody would hold your page content. This is a copy where we pulled json from one of our API's
var message = "try";
var jsonBody = "";
var request = require('request');
request.get({
url: "https://superduperwebaddress.com/api/pull"},
function(error,response,body){
if(!error)
{
var mybody = JSON.parse(body);
jsonBody = mybody;
console.warn("we are here well");
}else{
console.error(error);
}
}
);

Shopify. Problems updating Metafields with shopify-node-api

I'm having difficulty in updating an existing metafield with Shopify API. Each time I receive an error, advising me that the variant already exists... so it must be thinking I'm trying to create a new one (and not update).
I thought this may be an issue with 'put' and 'post' - so changed my method to put, however the error persists. I've hardwired in all my variables to make it easier to test.
I'm working with Cloudinary. I'm using https://github.com/sinechris/shopify-node-api with Express.js
app.post('/upload', function(req, res){
// upload page... assume we have uploaded our image - but have hard-wired a local file in for now
cloudinary.uploader.upload('/Users/Rob/Pictures/testimg.jpg', function(savedImg) {
var imageURL = savedImg.url;
console.log(imageURL)
},
{
public_id: "testimg"
});
// the saved image is returned - so we add it to our updateMetafieldData json object
var updateMetafieldData = {
"variant": {
"id": '253818949',
"metafields": [
{
"key": "variant_image_0",
"value": 'testimg', // whatever the public id of our image is.
"value_type": "string",
"namespace": "variant_image"
}
]
}
}
// and post the result to shopify - then redirect to /getvariants
Shopify.put('/admin/variants/253818949.json', updateMetafieldData, function(data){
// res.redirect('/getvariants')
});
});
I actually created Shopify Node API and was just now happened upon this months later but thought I'd answer for anyone else coming along.
Take a look at the shopify API here: https://docs.shopify.com/api/metafield#update
You can update the metafield directly by performing a PUT request against the metafield resource instead of the variant like so:
/admin/metafields/#{id}.json
You would of course need to know the ID of the metafield first so that would require a call to the variant first or you could simply store the id in your local database for reference.

Resources