简体   繁体   English

Salesforce 使用节点 JS 进行身份验证 API 使用访问令牌

[英]Salesforce Authentication using Node JS API With Access Token

I have a requirement to retrieve the Auth Code from SalesForce API.我需要从 SalesForce API 中检索 Auth Code。 I have created the scenario in Postman as below.我在 Postman 中创建了如下场景。

  1. Configuring the new Token using the below parameters使用以下参数配置新令牌

代币配置

  1. Once I press "Get New Access Token", Postman opens a popup and asks to type the Username and Password in the login prompt.一旦我按下“获取新访问令牌”,Postman 就会打开一个弹出窗口,并要求在登录提示中输入Username名和Password It shows the login page to SalesForce.它显示了 SalesForce 的登录页面。

  2. Once login success, Postman asks to use the token and it will be added here(See below image)登录成功后,Postman 会要求使用该令牌,并将其添加到此处(见下图) 访问令牌

  3. Then I hit the endpoint with the JSON body as a POST request.然后我使用 JSON 主体作为 POST 请求到达端点。

I need to recreate this scenario in NodeJS in order to work the whole process as a single process bypassing all the login prompts.我需要在 NodeJS 中重新创建这个场景,以便将整个过程作为一个单独的过程来工作,绕过所有的登录提示。

I am using the below method to initiate this task in order to get the Token.我正在使用以下方法来启动此任务以获取令牌。 However, the resources I found didn't match my requirement.但是,我找到的资源不符合我的要求。

As the first step, I used salesforce-oauth2 npm package as below.作为第一步,我使用了salesforce-oauth2 npm package,如下所示。

oauth2 = require('salesforce-oauth2');

var callbackUrl = "https://test.salesforce.com/services/oauth2/success",
consumerKey = "3MVG9sLbBx**********************2Qi.v***Vlhg3",
consumerSecret = "3MV**bBx**********************2Qi.v***Vlhg3";
    var uri = oauth2.getAuthorizationUrl({
        redirect_uri: callbackUrl,
        client_id: consumerKey,
        scope: 'api', // 'id api web refresh_token'
        // You can change loginUrl to connect to sandbox or prerelease env.
        //base_url: 'https://test.my.salesforce.com'
    });
    return response.redirect(uri);

When I debug I above code, it returns a URL pointing to the login page.当我调试上面的代码时,它返回一个指向登录页面的 URL。 I didn't want to pass this step since my requirement is to get the Auth-Code without opening any intermediate authentication popups.我不想通过这一步,因为我的要求是在不打开任何中间身份验证弹出窗口的情况下获取 Auth-Code。

How can I proceed with this?我该如何进行呢? Any idea to program until the 3rd step to get the Auth Token from the SalesForce API?任何想法编程直到第三步从 SalesForce API 获取 Auth 令牌?

Thanks in advance.提前致谢。

You tagged this .您标记了这个 It matters, is it really for community ("experience cloud") users or internals?重要的是,它真的是针对社区(“体验云”)用户还是内部人员? Salesforce has lots of OAuth2 flows to chose from: https://help.salesforce.com/articleView?id=sf.remoteaccess_oauth_flows.htm&type=5 Salesforce 有许多 OAuth2 流可供选择: https://help.salesforce.com/articleView?id=sf.remoteaccess_othauth

If you know the password and it's internal user (maybe real human, maybe you have some dedicated "Integration User") - you can work with Username-Password flow.如果您知道密码并且它是内部用户(也许是真人,也许您有一些专门的“集成用户”) - 您可以使用用户名密码流程。 There's no login page and no OAuth consent step.没有登录页面,也没有 OAuth 同意步骤。 But

This flow doesn't support scopes or refresh tokens.此流程不支持范围或刷新令牌。 Experience Cloud sites don't support the OAuth 2.0 username-password flow. Experience Cloud 站点不支持 OAuth 2.0 用户名-密码流。

You might be able to use JWT Flow .您也许可以使用JWT Flow You need username (no password) and your Node app would be sending a message signed with certificate that you uploaded earlier to SF "connected app".您需要用户名(无密码),并且您的 Node 应用程序将发送一条使用您之前上传到 SF“已连接应用程序”的证书签名的消息。 You could even mark the users as preauthorised so there's no consent screen.您甚至可以将用户标记为预授权,这样就没有同意屏幕。

Other than that I think all OAuth2 flows available for community need a human to actually type the password in. You can pass login hint in the url to save them the username but pass they need to provide on SF login page before coming back to your app.除此之外,我认为社区可用的所有 OAuth2 流程都需要人工输入密码。您可以在 url 中传递登录提示以保存用户名,但在返回您的应用程序之前传递他们需要在 SF 登录页面上提供.

Dig a bit in help, happy to be proven wrong.挖掘一点帮助,很高兴被证明是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM