最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Salesforce OAuth with REST API using Javascript - Stack Overflow

programmeradmin3浏览0评论

I have an app in my salesforce developer account that I want to allow my users to access from a remote app that I am building. I see that I must use OAuth2.0 to first authorize my users before they are allowed to access the salesforce data. At the moment I am trying to use the username-password OAuth flow described on salesforce.

Step 1) I request access token using username and password via the below code snippet

var password = 'userPassword' + 'securityToken'
$.ajax({
    type: 'GET',
    url: '',
    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', '[email protected]'),
        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
    }
});

My request, however, is failing with the following message:

1) OPTIONS  400 (Bad Request) 

2) XMLHttpRequest cannot load . No 
  'Access- Control-Allow-Origin' header is present on the requested resource. 
   Origin   http://localhost is therefore not allowed access. 

I have seen some sources (here here here) mention that CORS is not supported in salesforce, and that another solution should be used. Some solutions I have seen are Salesforce APEX code, AJAX toolkit, or ForceTK.

In summary, I am looking to see if (1) there is a simple mistake that I am making with my above request to get the OAuth access_token (2) or if I need to do something different to get the access (3) is there a better way to login users and access their salesforce data from my connected app?

All and any help is appreciated!

I have an app in my salesforce developer account that I want to allow my users to access from a remote app that I am building. I see that I must use OAuth2.0 to first authorize my users before they are allowed to access the salesforce data. At the moment I am trying to use the username-password OAuth flow described on salesforce.

Step 1) I request access token using username and password via the below code snippet

var password = 'userPassword' + 'securityToken'
$.ajax({
    type: 'GET',
    url: 'https://login.salesforce./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', '[email protected]'),
        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
    }
});

My request, however, is failing with the following message:

1) OPTIONS https://login.salesforce./services/oauth2/token 400 (Bad Request) 

2) XMLHttpRequest cannot load https://login.salesforce./services/oauth2/token. No 
  'Access- Control-Allow-Origin' header is present on the requested resource. 
   Origin   http://localhost is therefore not allowed access. 

I have seen some sources (here here here) mention that CORS is not supported in salesforce, and that another solution should be used. Some solutions I have seen are Salesforce APEX code, AJAX toolkit, or ForceTK.

In summary, I am looking to see if (1) there is a simple mistake that I am making with my above request to get the OAuth access_token (2) or if I need to do something different to get the access (3) is there a better way to login users and access their salesforce data from my connected app?

All and any help is appreciated!

Share Improve this question edited Apr 13, 2017 at 12:54 CommunityBot 11 silver badge asked Jul 29, 2014 at 17:30 Jaskaye17Jaskaye17 6371 gold badge12 silver badges22 bronze badges 4
  • Your approach to implement OAuth2.0 entirely in the front end will always face the CORS problem. – PM 77-1 Commented Jul 29, 2014 at 17:34
  • 1 The error is indeed because CORS is not supported. So, you are right, you need to handle it differently. You need server-side code to handle this. – Jeroen Noten Commented Jul 29, 2014 at 17:34
  • Is there a best approach to handling OAuth for salesforce server-side? Salesforce APEX code or AJAX toolkit? I tried using [salesforce.stackexchange./questions/17382/… example) and get a "Refused to set unsafe header "User-Agent" message. – Jaskaye17 Commented Jul 29, 2014 at 18:36
  • Is the AJAX Toolkit available to external apps or only to VisualForce pages? – jlewkovich Commented Jul 27, 2015 at 21:22
Add a ment  | 

4 Answers 4

Reset to default 7

You will need to handle the OAUTH part on your own server. This isn't just due to lack of CORS, there is also no way to securely OAUTH purely on the client-side. The server could really be anything but here is an example server written in Java using Play Framework which has a JavaScript / AngularJS client as well: http://typesafe./activator/template/reactive-salesforce-rest-javascript-seed

You can not make this request from JavaScript. You'll need to make a server side request. There are many implementations of this flow in PHP, C#, Java, etc.

I'm posting my ajax code here that has worked for me and this CORS error in console doesn't matter. If you see in network you will get the access token. see the ajax code below.

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./services/oauth2/token',
    type: 'POST',
    data: param,
    dataType: "json",
    contentType: "application/x-www-form-urlencoded",
    success: function (data) {
        alert(data);
    }

});
}

I hope this will work for you perfectly.

I think you need to add the origin URL/IP in CORS setting as well in salesforce if you are making a request from Javascript app so it can get access to salesforce data.

发布评论

评论列表(0)

  1. 暂无评论