oauth-2.0 NodeJ中的Salesforce身份验证

6fe3ivhb  于 2022-10-31  发布在  其他
关注(0)|答案(1)|浏览(186)

我正在尝试向NodeJS应用程序验证Salesforce。我收到类似错误的错误:invalid_grant - authentication failure.这里我还遗漏了什么.我需要从salesforce端做任何配置吗?这是我的代码.有人能帮我吗?

app.js
 var nforce = require('nforce');

    const client_id = '**'
    const client_secret = '****'
    const redirect_uri = 'https://***.sandbox.my.salesforce.com/services/oauth2/success'
    const sfdc_user = '*****'
    const sfdc_pass = '***'

    const credentials = {
        client_id :client_id,
        client_secret:client_secret,
        grant_type:"password",
        username:sfdc_user,
        password:sfdc_pass
    }

    async function getConnection(){
        const loginUrl = "https://***.sandbox.my.salesforce.com/services/oauth2/token";

        var org = nforce.createConnection({
            clientId: credentials.client_id,
            clientSecret: credentials.client_secret,
            redirectUri: redirect_uri,
        });
        console.log('org >>'+JSON.stringify(org));
        let oauth= await org.authenticate({ username: credentials.username, password: credentials.password});
        console.log('oauth >>'+oauth); //Couldnt get this console
        const access_token = oauth.access_token;
        const sf_auth_url = oauth.instance_url + '/services/data/v48.0/'
        sf_auth = {
            'Authorization':'Bearer ' + access_token, 
            'Content-type': 'application/json',
            'Accept-Encoding': 'gzip'
        }
        return { sf_auth,sf_auth_url }
    }

    module.exports = { getConnection } 

main.js
const f = require('./app');
const https = require('https')
const fs = require('fs')
const port = 3000

const server = https.createServer(function(req,res){
res.writeHead(200,{'Content-Type': 'text/html'})

res.end();
})

server.listen(port,function(error){
    if(error){
        console.log('Something Went Wrong!')
    }else{
        console.log('Server is listening on port '+port)
        f.getConnection();
    }
})
u4dcyp6a

u4dcyp6a1#

我收到此错误时,通常是因为没有将用户的安全令牌附加到密码字段的末尾。这是在您首次设置密码或上次执行密码重置时通过电子邮件发送给您的令牌。如果您需要重置它,您可以通过个人设置重置我的安全令牌
Reset Your Security Token
如果不是这样,这个用户名/密码身份验证文档应该会有所帮助。
OAuth 2.0 Username-Password Flow for Special Scenarios

相关问题