-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using jsforce with client credentials workflow #1371
Comments
Hi @poudelroshan, if you are using jsforce v2, here's an example of how to use the client credentials flow: If using v1, you could extend the Line 14 in 7c8e6a1
|
I'm just starting to use jsforce. In the docs, there is still no example on how to get an How can this be done? I have no username, no password and no JWT Bearer. Only what @poudelroshan described in the issue description. I want to get an access_token for machine to machine communication. |
For reference, here is Salesforce's documentation on authenticating with the OAuth Client Credentials Flow. Here's an example of using it with jsforce in Deno, which should be easy enough to adapt to Node.js. Note that import jsforce from "npm:jsforce@^3.2.0";
import { clientId, clientSecret, loginUrl } from "./config.ts";
const conn = new jsforce.Connection({
oauth2: { clientId, clientSecret, loginUrl },
});
const userInfo = await conn.authorize({ grant_type: "client_credentials" });
console.log(userInfo);
const result = await conn.query(`SELECT Id, Name FROM Account LIMIT 1`);
console.log(result.records); |
Note that Salesforce does not support refresh tokens with the client credentials flow, so jsforce will not automatically obtain a new access token if the previous one expires or is revoked. Here's an example of initializing a const conn = new jsforce.Connection({
serverUrl: loginUrl,
oauth2: { clientId, clientSecret, loginUrl },
async refreshFn(conn, callback) {
try {
await conn.authorize({ grant_type: "client_credentials" });
callback(null, conn.accessToken!);
} catch (err) {
callback(err);
}
},
});
const result = await conn.query(`SELECT Id, Name FROM Account LIMIT 1`);
console.log(result.records); Note that the const conn = new jsforce.Connection({
oauth2: { clientId, clientSecret, loginUrl },
async refreshFn(conn, callback) {
try {
await conn.authorize({ grant_type: "client_credentials" });
callback(null, conn.accessToken!);
} catch (err) {
callback(err);
}
},
});
await conn.authorize({ grant_type: "client_credentials" });
const result = await conn.query(`SELECT Id, Name FROM Account LIMIT 1`);
console.log(result.records); |
I couldn't find documentation for using jsforce with client credentials workflow. Is it supported?
Using postman, I do the following to receive access token:
Then I send requests using:
The text was updated successfully, but these errors were encountered: