GithubHelp home page GithubHelp logo

jsforceinaction's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsforceinaction's Issues

Can not connect with jsForce

Hello.

I am currently writing a REST service that connect to Salesforce via jsForce with APEX REST requests.

To do so, I write code inspired by yours except that I am using a local cache service in place of a Redis server.

My routes:

// /callback
// Never reached by a request
const callback = function(request, result, next) {
    const code = request.params.code;

    sfService.processOAuthCode(code)
      .then(result.redirect('/explorer', next))
      .catch(result.redirect('error.html', next))
}

// /oauth2/auth
// Never reached by a request
const oauth2auth = function(request, result, next) {
  result.redirect(sfService.getAuthorizationUrl(), next);
}

My sfService:

const getConnection = function() {

    return cacheService.get('AuthorizationInfo')
    .then(authInfo => {
        const options = { version };

        if(authInfo) {
          options.instanceUrl = authInfo.instanceUrl;
          options.accessToken = authInfo.accessToken;
          options.oauth2 = {
            clientId,
            clientSecret,
            redirectUri:  `${protocol}://${host}:${port}/${callback}` // http://localhost:8000/callback at the moment
          };
        } else {
          options.oauth2 = getOAuth2()
        }

        const connection = new Jsforce.Connection(options);

        Bluebird.promisifyAll(Object.getPrototypeOf(connection))

        return connection
    })
    .catch(error.logAndThrow('Error encountered in getConnection'));
}

const getAuthorizationUrl = function() {
  return getOAuth2().getAuthorizationUrl({scope: 'api id web'});
}

const processOAuthCode = function(code) {  
  return cacheService.drop('AuthorizationInfo')
  .then(() => getConnection())
  .then(connection => [connection, connection.authorizeAsync(code)])
  .spread((connection, userInfo) => {
    return cacheService.set('AuthorizationInfo', {
      accessToken: connection.accessToken,
      instanceUrl: connection.instanceUrl,
      userId: userInfo.id,
      orgId: userInfo.organizationId
    });
  })
  .catch(error.logAndThrow('Error encountered in processOAuthCode'));
}

When I am trying to fetch data using as callback of one of my route

 getConnection()
 .then(connection => {
    return connection.requestGetAsync('/apexrest/pricebook?pricebookName=standard')
    .catch(error.logAndThrow('Can not fetch data'))
  })

I still have an error "resource" not found. If I print the connection content, I do not have any accessToken and it seems that my callbackUri http://localhost:8000/callback has never been reached by a request from Salesforce.

Do you have an idea where I am doing wrong ?

Thank you in advance :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.