Dwolla : InvalidTokenType : The requested endpoint requires an account token


#1

Now I am switching to production. And getting “InvalidTokenType : The requested endpoint requires an account token” error while creating new customer.

'getDwollaAccount' : function(userId) {
   var dwollaCredentials = getDwollaCreden();
   const client = new dwolla.Client({
     key         : dwollaCredentials.appKey,
     secret      : dwollaCredentials.appSecret,
     environment : 'production' // optional - defaults to production
   }); // call dwolla initial api

   client.auth.client()
   .then(Meteor.bindEnvironment(function(appToken) {

    var userFound = 
    Meteor.users.findOne({'_id':userId,'profile.dwollaLocation':''});
    if(userFound){
        var requestBody = {
          firstName : userFound.profile.firstname,
          lastName  : userFound.profile.lastname,
          email     : userFound.emails[0].address
        };
        appToken
            .post('customers', requestBody)
            .then( 
                (res)=> {
                    var dwollaLocation = res.headers.get('location');;      
                    return Promise.resolve(dwollaLocation);
                }
            )
            .then(
                Meteor.bindEnvironment((dloc) =>{ 
                        return Promise.resolve(dloc);
                })
            )
            .catch(Meteor.bindEnvironment((error) => {
               console.log(error);
                              }));  
    } // end of user found
    })
    );
    },

Thanks in advance!


#2

Caveat: I’m unfamiliar with dwolla, and I may have skipped or misunderstood something, but I wonder if it’s to do with trying to return results from with a Promise then method?

It’s much easier to use async/await when Promises are involved. Everything then becomes linear. This is my attempt to rewrite your method:

async getDwollaAccount(userId) {
  const dwollaCredentials = getDwollaCreden();
  const client = new dwolla.Client({
    key: dwollaCredentials.appKey,
    secret: dwollaCredentials.appSecret,
    environment: 'production' // optional - defaults to production
  }); // call dwolla initial api

  const userFound = Meteor.users.findOne({ '_id': userId, 'profile.dwollaLocation': '' });
  if (userFound) {
    try {
      const appToken = await client.auth.client();
      const requestBody = {
        firstName: userFound.profile.firstname,
        lastName: userFound.profile.lastname,
        email: userFound.emails[0].address
      };
      const res = await appToken.post('customers', requestBody);
      const dwollaLocation = await res.headers.get('location');
      return dwollaLocation;
    } catch (err) {
      throw new Meteor.Error('Error', err.message);
    }
  }
},

Obviously, that needs checking - I’ve assumed you want dwollaLocation as the result of your method.


#3

Thank you so much for reply. I will surely give a try with your code. And yes dwollaLocation is result of method.