Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Salesforce OAuth与使用Javascript的REST API_Javascript_Ajax_Rest_Oauth_Salesforce - Fatal编程技术网

Salesforce OAuth与使用Javascript的REST API

Salesforce OAuth与使用Javascript的REST API,javascript,ajax,rest,oauth,salesforce,Javascript,Ajax,Rest,Oauth,Salesforce,我的salesforce开发者帐户中有一个应用程序,我希望允许我的用户从我正在构建的远程应用程序访问该应用程序。我发现我必须先使用OAuth2.0授权我的用户,然后才允许他们访问salesforce数据。目前,我正在尝试使用上描述的用户名密码OAuth流 步骤1)我通过下面的代码片段使用用户名和密码请求访问令牌 var password = 'userPassword' + 'securityToken' $.ajax({ type: 'GET', url: 'https://l

我的salesforce开发者帐户中有一个应用程序,我希望允许我的用户从我正在构建的远程应用程序访问该应用程序。我发现我必须先使用OAuth2.0授权我的用户,然后才允许他们访问salesforce数据。目前,我正在尝试使用上描述的用户名密码OAuth流

步骤1)我通过下面的代码片段使用用户名和密码请求访问令牌

var password = 'userPassword' + 'securityToken'
$.ajax({
    type: 'GET',
    url: 'https://login.salesforce.com/services/oauth2/token',
    contentType: 'application/json',
    dataType: 'json',
    beforeSend: function(xhr) {
        xhr.setRequestHeader('grant_type','password'),
        xhr.setRequestHeader('client_id',  '<client_id_here>'),
        xhr.setRequestHeader('client_secret', '<client_secret_here'),
        xhr.setRequestHeader('username', 'username@location.com'),
        xhr.setRequestHeader('password', "password")
    },
    success: function(response) {
        console.log('Successfully retrieved ' + response);
        //Other logic here
    },
    error: function(response) {
        console.log('Failed ' + response.status + ' ' + response.statusText);
        //Other logic here
    }
});
我看到一些消息来源(这里)提到salesforce不支持CORS,应该使用另一种解决方案。我看到的一些解决方案是Salesforce APEX代码,或

总之,我想看看(1)我的上述获取OAuth access_令牌的请求是否犯了一个简单的错误(2)或者我是否需要做一些不同的事情来获取访问权限(3)是否有更好的方法登录用户并从我连接的应用程序访问他们的salesforce数据


感谢您的帮助

您不能从JavaScript发出此请求。您需要发出服务器端请求。此流程在、等中有许多实现。

您需要在自己的服务器上处理OAUTH部分。这不仅仅是因为缺少CORS,也没有办法完全在客户端安全地OAUTH。服务器可以是任何东西,但这里有一个使用Play Framework用Java编写的示例服务器,它也有一个JavaScript/AngularJS客户机:

我在这里发布了我的ajax代码,这些代码对我有用,控制台中的这个CORS错误无关紧要。如果您在网络中看到,您将获得访问令牌。 请参阅下面的ajax代码

function gettoken()
{
    var param = {
      grant_type: "password",
      client_id : "id here",
      client_secret : "seceret here ",
      username:"username",
      password:"password with full key provided by sf"};
$.ajax({
    url: 'https://test.salesforce.com/services/oauth2/token',
    type: 'POST',
    data: param,
    dataType: "json",
    contentType: "application/x-www-form-urlencoded",
    success: function (data) {
        alert(data);
    }

});
}

我希望这将对您非常有效。

我认为如果您从Javascript应用程序发出请求,您需要在CORS设置中以及在salesforce中添加源URL/IP,以便它能够访问salesforce数据。

您完全在前端实现
OAuth2.0
的方法将始终面临
CORS
问题错误确实是因为不支持CORS。所以,你是对的,你需要以不同的方式处理它。您需要服务器端代码来处理此问题。对于salesforce服务器端,是否有处理OAuth的最佳方法?Salesforce APEX代码还是AJAX工具包?我尝试使用[example]并获得一个“拒绝设置不安全头”的用户代理"消息。AJAX工具包是可用于外部应用程序还是仅可用于VisualForce页面?我从Salesforce发出的所有请求都是这样,因为它们不允许CORS?还是在我使用服务器端请求通过OAuth过程后,我可以从Javascript访问它们的RESTful API?一旦您通过OAuth处理并拥有活动访问令牌,您应该能够从客户端发出API请求。有关更多信息,请参阅此页面:当然,响应包含令牌。但是不允许任何JS访问和使用此令牌。因此,是的……您的回答是对的,但不能解决问题本身……这是一个注释而不是答案。
function gettoken()
{
    var param = {
      grant_type: "password",
      client_id : "id here",
      client_secret : "seceret here ",
      username:"username",
      password:"password with full key provided by sf"};
$.ajax({
    url: 'https://test.salesforce.com/services/oauth2/token',
    type: 'POST',
    data: param,
    dataType: "json",
    contentType: "application/x-www-form-urlencoded",
    success: function (data) {
        alert(data);
    }

});
}