JSforce not integrating with express in Node - express

To give some context, I am a front end dev tasked with intergrating salesforce into a react app. This is a new learning curve for me as I am a SF newbie. I have been looking for a way to make intergration easier and I came across a video that showed how I can use a node packaged called JSforce to auth and fetch data from SF to my node express backend. I did as the video suggested but something appears to be not working as I am not console logging anything. Can anyone who has experience in using Jsforce take a look at my code below and let me know where I have gone wrong?
const express = require('express');
const app = express();
const port = 5000;
const jsforce = require('jsforce');
const username = 'blah';
const password = 'blah+ security token';
var conn = new jsforce.Connection({
// you can change loginUrl to connect to sandbox or prerelease env.
loginUrl: 'https://tahina-test2-dev-ed.my.salesforce.com/'
});
conn.login(username, password, function(err, userInfo) {
if (err) {
return console.error(err);
}
// Now you can get the access token and instance URL information.
// Save them to establish connection next time.
console.log(conn.accessToken);
console.log(conn.instanceUrl);
// logged in user property
console.log('User ID: ' + userInfo.id);
console.log('Org ID: ' + userInfo.organizationId);
// ...
conn.sobject('Account').retrieve('0012X000022HhE5QAK', function(err, account) {
if (err) {
return console.error(err);
}
console.log('Name : ' + account.Name);
// ...
});
});
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

Related

Market data routing to frontend: Alpaca WebSocket -> Node.js WebSocket -> React Frontend

I'm trying to use the websocket example from:
https://github.com/alpacahq/alpaca-trade-api-js/blob/master/examples/websocket_example_datav2.js
In order to connect to the Alpaca V2 data stream. Currently, my stream is working but I'm trying to route my data to the client side using Server Sent Events. My data flow seems like it should be:
Alpaca Data Stream API -> My Node.js server -> React Frontend.
The issue I have is using the DataStream object in the example in order to route the data to the frontend. Since, with the object alone, I don't have any route to subscribe to via Server Sent Events, does this mean that I should also be using either express, socket.io, or ws? Since the all of the ".on_xyz" methods are defined within the DataStream object, I'm not sure how to set up the endpoint properly to allow my frontend to subscribe to it. If anyone knows how to route this datastream information forward it would be greatly appreciated- I'm particularly trying to work with the .onStockQuote method but any of them is fine! I'm simply trying to use Node as an inbetween router so that I don't have to subscribe directly from the frontend (and not use the sdk), because that limits scalability of the API's use.
"use strict";
/**
* This examples shows how to use tha alpaca data v2 websocket to subscribe to events.
* You should use the alpaca api's data_steam_v2, also add feed besides the other parameters.
* For subscribing (and unsubscribing) to trades, quotes and bars you should call
* a function for each like below.
*/
import express from 'express';
const app = express()
const Alpaca = require("#alpacahq/alpaca-trade-api");
const API_KEY = "XYZ_Key";
const API_SECRET = "XYZ_Secret";
const PORT = 3000;
// Add a new message and send it to all subscribed clients
const addMessage = (req, res) => {
const message = req.body;
// Return the message as a response for the "/message" call
res.json(message);
return ;
};
class DataStream {
constructor({ apiKey, secretKey, feed }) {
this.alpaca = new Alpaca({
keyId: apiKey,
secretKey,
feed,
});
const socket = this.alpaca.data_stream_v2;
socket.onConnect(function () {
console.log("Connected");
socket.subscribeForQuotes(["AAPL"]);
// socket.subscribeForTrades(["FB"]);
// socket.subscribeForBars(["SPY"]);
// socket.subscribeForStatuses(["*"]);
});
socket.onError((err) => {
console.log(err);
});
socket.onStockTrade((trade) => {
console.log(trade);
});
socket.onStockQuote((quote) => {
console.log(quote);
});
socket.onStockBar((bar) => {
console.log(bar);
});
socket.onStatuses((s) => {
console.log(s);
});
socket.onStateChange((state) => {
console.log(state);
});
socket.onDisconnect(() => {
console.log("Disconnected");
});
socket.connect();
// unsubscribe from FB after a second
// setTimeout(() => {
// socket.unsubscribeFromTrades(["FB"]);
// }, 1000);
}
}
app.post("/message", addMessage);
let stream = new DataStream({
apiKey: API_KEY,
secretKey: API_SECRET,
feed: "sip",
paper: false,
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});

OctoKit with Auth0 (Github Login) in NextJS

I am building a Next JS app that has Github Login through Auth0 and uses the Octokit to fetch user info / repos.
In order to get the IDP I had to setup a management api in auth0. https://community.auth0.com/t/can-i-get-the-github-access-token/47237 which I have setup in my NodeJs server to hide the management api token as : GET /getaccesstoken endpoint
On the client side : /chooserepo page, I have the following code :
const chooserepo = (props) => {
const octokit = new Octokit({
auth: props.accessToken,
});
async function run() {
const res = await octokit.request("GET /user");
console.log("authenticated as ", res.data);
}
run();
And
export const getServerSideProps = withPageAuthRequired({
async getServerSideProps({ req, params }) {
let { user } = getSession(req);
console.log("user from get session ", user);
let url = "http://localhost:4000/getaccesstoken/" + user.sub;
let data = await fetch(url);
let resData = await data.text();
return {
props: { accessToken: resData }, // will be passed to the page component as props
};
},
});
However, I keep getting Bad credentials error. If I directly put the access token in the Octokit it seems to work well, but doesn't work when it's fetching the access token from the server.
It seems like Octokit instance is created before server side props are sent. How do I fix it ?
I figured out the error by comparing the difference between the request headers when hardcoding and fetching access token from server. Turns out quotes and backslashes need to be replaced (and aren't visible when just console logging)

React native IAP (Android) Validation receive Problem

I added react-native-iap library( https://github.com/dooboolab/react-native-iap ) for using subscriptions.
Work well on IOS but I have some issues on Android.
On Android side perfectly working subscription person. I need to check expired date for block expired users. For IOS side I use RNIap.validateReceiptIos(receiptBody, false) this give me expired date and expired_ms. I used these but on Android side I use
const result = await RNIap.validateReceiptAndroid(
'com.test.app',
'test_sku_1m_acc',
//below token comes from RNIap.getPurchaseHistory() -> purchaseToken
'faolcmekaiihplkgoaljnfjh.AO-J1OxykzAlHtDtn0-NRHGUBavVbhA_bBLVYPIlc4yUMkHTLXrA_V66lBsZYKknGt-gcCZgznCXKNww0BW9FLuf4ah23HhB_Q',
//below token comes from localhost node.js backend (nodejs Code *)
'ya29.c.Kp0B7QcDz53Bt4YeXHZ6lOjSm7NW9FW0pNwSgX78EQVYNlhSoY2ZM-lhug4mODUy8lzghDuJ8SVj9Lw1Dtvh8ZuB8YFEEv_phTc_YOwJ2PK1_WPyS3nRuF-B96wJADN0F6QBCShHl2uOxEAocNyoCKzb6k1YVPBZxENGS6kwBXfcnNR_Hu4-TKpnl3y0s6vX5_XE_viR46d9cO73cdXIjA',
true, // for subscriptions should pass "true"
).catch((err) => {
console.log('err', JSON.stringify(err));
})
the code returns {"statusCode":401,"line":126336,"column":53,"sourceURL":"http://192.168.2.27:8081/index.bundle?platform=android&dev=true&minify=false"}
This problem is from me or from library. Thank you for your response.
Note: node.js Code *:
const open = require("open");
const { google } = require("googleapis");
const account = require("./service-account.json");
const express = require("express");
const app = express();
const JWTClient = new google.auth.JWT(
account.client_email,
null,
account.private_key,
["https://www.googleapis.com/auth/androidpublisher"]
);
app.get("/", (req, res) => {
JWTClient.getAccessToken((err, token) => {
if (err) {
return res.status(404).send("get access token failed");
}
return res.status(200).send(token);
});
});
app.listen(8000);
open("http://localhost:8000");

How use the #c8y/client library

I am testing the new #c8y/client library for typescript.
I have a very simple code :
import {
Client
} from '#c8y/client';
//const baseUrl = 'https://bismark1.cumulocity.com/';
const baseUrl = 'https://demos.cumulocity.com/';
const tenant = 'bismark1';
const user = '...';
const password = '.....';
(async() => {
console.log('authentication to c8y server')
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
console.log('result from authetication', client)
const {
data,
paging
} = await client.inventory.list();
console.log('result from inventory ', data)
// data = first page of inventory
const nextPage = await paging.next();
// nextPage.data = second page of inventory
const managedObjId: number = 1;
(async() => {
const {
data,
res
} = await client.inventory.detail(managedObjId);
console.log(data)
})();
})();
When I run the .js compiled form the .ts file I get the response below :
authentication to c8y server
And then the execution stops.
The line
console.log('result from authetication', client)
is never called. Seems like something fails in the authentication process and not error is showed.
What I'm doing wrong ?
Thanks.
The first problem might be CORS. You need to enable it if you want to request from a different domain. Here is a guide how to do that in Cumulocity:
Under "Access control", administrators can enable cross-origin
resource sharing or "CORS" on the Cumulocity API.
The second problem could be that you are not running it from a local development server. I mostly use this http-server from npm to quickly test scripts. You can use it the following way:
$ npm install http-server -g
$ http-server
If that all is not helping you might try catch the client to see the error it is throwing:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}
The exeption might tell you more about what is wrong with your code or if there is a bug in the client.

passport-jwt, not throing error, but not authenticating. wont even console.log

I am trying to authenticate using passport-jwt, express etc.
I currently am not getting authenticated, but most troubling is that I cant seem to console.log inside the function... nothing happens.
Here is the full passport.js
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config/database');
module.exports = function(passport) {
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromHeader();
opts.secretOrKey = config.secret;
console.log(opts); // THIS SHOW PRIORE TO THE EXPRESS LISTEN MSG!
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
console.log(jwt_payload); // THIS DOESNT EVER SHOW!
User.getUserById(jwt_payload._id, (err, user) => {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
// or create a new account
}
});
}));
}
the users.js
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
const config = require('../config/database');
const User = require('../models/user');
...code removed for brevity...
router.get('/profile', passport.authenticate('jwt', {session:false}), (req,res,next) => {
res.json({user: req.user});
});
module.exports = router;
Any ideas on how I can 'see' my payload so I can find whats going on?
Im using postman to hit 'http://localhost:3000/users/profile'
passing headers "authentication" and the token "JWT my-token..."
THx - I realize this is a bit of an odd request...The tutorial I was following, indicates I should be able to see the payload, even if i am not authenticated...
So Im kinda stuck with no idea where to turn.
UGH - this is the second time I've continued to plug away at something after I asked, only to hit upon a solution. apperantly the version Im am using is newer than the examples and ExtractJwt.fromHeader() should be .fromAuthHeaderWithScheme("jwt"). It is frustrating that it did not error, and did not progress enough to log, however - NOW I get my console log, and can see my payload... I hope this helps someone else. (I am frustrated with the pace and changes made to js libs, that destroy backwards compatibility, and do not clearly document a path, I understand kinda why it happens, but it makes learning and troubleshooting very frustrating)