3

I'm running a node app on heroku that that I am trying to pull into a Canvas App, The Canvas App Preview error simply says "Cannot POST/". When I look at my node server debugger for local host it says similar: POST / HTTP/1.1" 404 140 "https://my-dev-ed.my.salesforce.com/. Since this is my first time integrating Node into Canvas, I really appreciate any help.

FWIW, I have been able to run the Quick Start java example from the SF developer guide and 2 other example Canvas Apps in my development sandbox.

I also have been able to run this example, https://www.jamesward.com/2016/01/13/salesforce-rest-apis-from-zero-to-cloud-to-local-dev-in-minutes, which wasn't a Canvas app, but it ran the locahost as http. Throwing one last dart, I have added nforce to my dependencies, like that tutorial, but my app is still getting the error in Canvas App Previewer. I am thinking that because I cannot run my localhost as https, when Canvas App Previewer pings my http:localhost, the return of my developer sandbox, https://my-dev-ed.my.salesforce.com/, it is being blocked.

I am really out of ideas, and I don't know how to more deeply debug this.

My Connected App has these things set:

Access Method:  Signed Request (POST)
Oauth Enabled with : Permitted Users    Admin approved users are pre-authorized
I am a System Admin

Here's my server.js.

var express = require('express');
var logger = require('morgan');
var nforce = require('nforce');
var hbs = require('hbs');
var PORT = process.env.PORT || 9001;

var app = express();

app.use(logger('combined'));

app.set('view engine', 'hbs');
app.enable('trust proxy');

function isSetup() {
  return (process.env.CONSUMER_KEY != null) && (process.env.CONSUMER_SECRET != null);
}

function oauthCallbackUrl(req) {
  return req.protocol + '://' + req.get('host');
}

app.get('/', function(req, res) {
  if (isSetup()) {
    var org = nforce.createConnection({
      clientId: process.env.CONSUMER_KEY,
      clientSecret: process.env.CONSUMER_SECRET,
      redirectUri: oauthCallbackUrl(req),
      mode: 'single'
    });

    if (req.query.code !== undefined) {
      org.authenticate(req.query, function(err) {
        if (!err) {

              res.render('index');

        }
        else {
          if (err.message.indexOf('invalid_grant') >= 0) {
            res.redirect('/');
          }
          else {
            res.send(err.message);
          }
        }
      });
    }
    else {
      console.log('req.query.code is undefined');
      res.redirect(org.getAuthUri());

    }
  }
  else {
    console.log('isSetup() is already run');
    res.render('index');
  }
});

app.get('/setup', function(req, res) {
  if (isSetup()) {
    res.redirect('/');
  }
  else {
    var isLocal = (req.hostname.indexOf('localhost') == 0);
    var herokuApp = null;
    if (req.hostname.indexOf('.herokuapp.com') > 0) {
      herokuApp = req.hostname.replace(".herokuapp.com", "");
    }
    res.render('setup', { isLocal: isLocal, oauthCallbackUrl: oauthCallbackUrl(req), herokuApp: herokuApp});
  }
});

app.listen(process.env.PORT || 9001);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.