5

I'm trying to register TAKE_A_NOTE with the 'mirror-api-subscription' event listener. I'm not having any luck: I can launch take a note with "my app", however, there's no console log that the event has been recognized.

I would like to recognize when the TAKE_A_NOTE function has occurred or finished, and handle the response afterwards. I do not know whether the subscription should be on a stream or if I am implementing the EventListener in a faulty manner. Your help would be greatly appreciated.

The code I am utilizing is:

// references
// http://www.recursiverobot.com/post/57348836217/getting-started-with-the-mirror-api-using-node-js
// https://www.npmjs.org/package/mirror-api-subscription

var express = require('express')
    , http = require('http')
    , https = require('https')
    , fs = require('fs')
    , googleapis = require('googleapis')
    , OAuth2Client = googleapis.OAuth2Client;
var app = express();
var oauth2Client = new OAuth2Client(process.env.MIRROR_DEMO_CLIENT_ID,
   process.env.MIRROR_DEMO_CLIENT_SECRET, process.env.MIRROR_DEMO_REDIRECT_URL);


// all environments
app.set('port', 8888);
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);

var gotToken = function () {
    googleapis
        .discover('mirror', 'v1')
        .execute(function (err, client) {
            if (!!err) {
                failure();
                return;
            }

            insertContact(client, failure, success);
            insertSubscription(client, failure, success);
        });
    };


var insertContact = function (client, errorCallback, successCallback) {
    client
        .mirror.contacts.insert({
            "id": "myapp",
            "displayName": "myApp",
            "priority": 100,
            "acceptCommands": [
                {"type": "TAKE_A_NOTE"}
            ],
            "speakableName":"my app"
        })
        .withAuthClient(oauth2Client)
        .execute(function (err, data) {
            if (!!err)
                errorCallback(err);
            else
                successCallback(data);
        });
    };

var insertSubscription = function (client, errorCallback, successCallback) {
    client.mirror.subscriptions.insert({
        "callbackUrl":"https://localhost:7777/notification",
        "collection":"timeline",
        "userToken":"001",
        "verifyToken":"secret",
        "operation":["INSERT"]
        });
    }

    var subscription = require('mirror-api-subscription')(
    function () {
    })

    subscription.on('locations#UPDATE',
    function (notification, user, payload) {
      console.log('location of user %s updated', user.id)
    })

    subscription.on('timeline#INSERT:LAUNCH',
    function (notification, user, payload) {
      console.log('subscription timeline#INSERT:LAUNCH')
    })

    subscription.on('timeline#UPDATE:CUSTOM',
    function (notification, user, payload) {
        console.log('subscription timeline#UPDATE:CUSTOM')
    })

    app.post('/notification', subscription.dispatcher())


    app.get('/', function (req, res) {
        if (!oauth2Client.credentials) {
            // generates a url that allows offline access and asks permissions
            // for Mirror API scope.
            var url = oauth2Client.generateAuthUrl({
                access_type: 'offline',
                scope: 'https://www.googleapis.com/auth/glass.timeline'
            });
            res.redirect(url);
        } else {
            gotToken();
        }
        res.write('Glass Mirror API with Node');
        res.end();

    });

    app.get('/oauth2callback', function (req, res) {
        // if we're able to grab the token, redirect the user back to the main page
        grabToken(req.query.code, failure, function () {
            res.redirect('/');
        });
    });

    app.post('/reply', function(req, res){
        console.log('replied',req);
        res.end();
    });

    var options = {
        key: fs.readFileSync('./ssl/key.pem'),
        cert: fs.readFileSync('./ssl/cert.pem'),
    };

    https.createServer(options, app).listen(7777, function() {
        console.log('https listening on 7777');
    });

    http.createServer(app).listen(app.get('port'), function () {
        console.log('Express server listening on port ' + app.get('port'));
    }); 
3
  • how does your notification file (at localhost:7777/notification) look like? I am trying to do the same as you are.. but am stuck at understanding what to give in my callback URL and the redirect URL (process.env.MIRROR_DEMO_REDIRECT_URL)?? Mar 13, 2014 at 12:46
  • Did the answer given by @Prisoner help you resolving the issue and get the timeline access from Node JS? Mar 13, 2014 at 14:43
  • @aug2uag - can you please indicate for the change you made to make it work?
    – kimo
    Jun 30, 2014 at 10:18

2 Answers 2

5

There are at least two potential problems with your code:

  1. The callbackUrl must be an internet accessible HTTPS address. "Localhost" isn't good enough, since Google's servers need to be able to resolve it. You can provide an IP address, if you have a public IP address, or use a tunnel.

  2. You don't do anything with the result of the call to client.mirror.subscriptions.insert(). Typically, you should call it the same way you call client.mirror.contacts.insert(), which is to chain it with withAuthClient() and execute(). You'll need to call execute() if you expect it to register with the Mirror service. See https://github.com/google/google-api-nodejs-client/ for documentation about the "googleapis" package and discovery service

  3. Possibly related to #2, but I'm not familiar with the package you're including with require('mirror-api-subscription'), which seems to handle things differently than the "googleapis" package and discovery service it offers. From the reference documentation for it, however, it isn't clear that it actually sets up the callback anywhere and is just there to verify the callbacks and dispatch to functions that do the work.

0
var insertSubscription = function (client, errorCallback, successCallback) {
client.mirror.subscriptions.insert({
    "callbackUrl":"https://mirrornotifications.appspot.com/forward?url=callbackURL",
    "collection":"timeline",
    "userToken":"001",
    "verifyToken":"secret",
    "operation":["INSERT"]
    }).withAuthClient(oauth2Client).execute(function (err, data) {
        if (!!err)
            errorCallback(err);
        else
            successCallback(data);
    });
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.