Salesforce Oauth重定向CORS问题节点Express应用程序 [英] Salesforce Oauth redirect CORS issue node express app

查看:193
本文介绍了Salesforce Oauth重定向CORS问题节点Express应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尽一切努力解决我收到的CORS错误.对于oauth流程,要将您的应用重定向到Salesforce登录屏幕(但是此网址会在后台自动将其从Salesforce重定向到Salesforce)

I've tried everything to get around the CORS error I am receiving. For oauth flow, to redirect your app to the Salesforce login screen (but this url auto redirects behind the scenes from salesforce to salesforce)

XMLHttpRequest无法加载salesforce url 2从salesforce重定向 salesforce oauth url 2的URL 1已被CORS策略阻止:否 请求中存在"Access-Control-Allow-Origin"标头 资源.因此,不允许使用来源" http://localhost:3000 " 访问.

XMLHttpRequest cannot load salesforce url 2 Redirect from salesforce url 1 to salesforce oauth url 2 has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我已经安装了CORS模块并尝试过此操作:

I've installed CORS module and also tried this:

app.use('/public',function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
// Request headers you wish to allow
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested- 
With, Content-Type, Accept");     
// Set to true if you need the website to include cookies in the  
requests sent   
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});

任何人都能真正获得这项工作并提供帮助吗?

Can anyone actually get this working and help?

推荐答案

几天前我刚刚写了一个博客:

I just wrote a blog couple of days back: http://nodexperts.com/blog/salesforce-oauth-using-nodejs/

首先,这将帮助您在Node-Express应用程序中设置OAuth2和SalesForce.

First of all, this will help you a lot in setting up OAuth2 and SalesForce in your Node-Express app.

另一件事是,您可以从应用程序-> SalesForce登录应用程序-> your_redirect_uri.

Another thing is that you can go from your app -> SalesForce login app -> your_redirect_uri.

从服务器到服务器执行此操作只会收到CORS错误.

因此,如果您的应用程序具有一些UI和一些用户交互,那么我建议您从客户端代码重定向到SalesForce页面.

So, if you have an app which has some UI and some user interaction, then I would suggest you to redirect to SalesForce page from your client code.

以下是如何使用 express roure和 jsforce npom软件包重定向到Salesforce登录页面的代码段:

Here's a snippet of how you can redirect to login page of Salesforce using express roure and jsforce npom package:

app.get('/oauth2/auth', function(req, res) {
  const oauth2 = new jsforce.OAuth2({
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET_ID,
    redirectUri: `${req.protocol}://${req.get('host')}/${process.env.REDIRECT_URI}`
  });
  res.redirect(oauth2.getAuthorizationUrl({}));
});

这是设置SalesForce连接的应用程序,重定向URL的方法:

And this is how you can set your SalesForce conncted app, redirect url:

app.get('/redirect_uri', function(req,res) {
  const oauth2 = new jsforce.OAuth2({
    clientId: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET_ID,
    redirectUri: `${req.protocol}://${req.get('host')}/${process.env.REDIRECT_URI}`
  });
  const conn = new jsforce.Connection({ oauth2 : oauth2 });

  conn.authorize(req.query.code, function(err, userInfo) {
    if (err) {
      return console.error(err);
    }
    console.log(conn.accessToken);
  });
});

希望一切都清楚,如有任何澄清,请通知我.

Hope, everything is clear, let me know in case of any clarification.

这篇关于Salesforce Oauth重定向CORS问题节点Express应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆