Salesforce REST API - 返回未授权的401

时间:2014-04-15 20:29:55

标签: salesforce

我正在编写一个简单的应用程序,可以使用Javascript [JQuery / Angular]从Web查询salesforce数据。我已尝试按照所有步骤(https://www.salesforce.com/us/developer/docs/api_rest/)进行操作,但在进行任何需要令牌的调用时,仍会收到401(未授权)错误。

我使用以下OAuth政策设置了已连接的应用:

OAuth政策     允许的用户:所有用户都可以自我授权     IP限制:放宽IP限制     要求用户登录:每次使用此应用程序     此应用程序有权:通过Web提供对您的数据的访问权限     此应用程序有权:访问和管理您的数据

我按照以下方式关注用户代理流程:

var url = "https://login.salesforce.com/services/oauth2/authorize?";
url += "response_type=token";
url += "&client_id="{my application id}";  // removed for this post
url += "&redirect_uri=https://localhost:8443/obsandbox/connect.html";
url += "&display=popup";
url += "&scope=api web";
window.location = url;

此流程似乎已更正,我进入登录页面并重定向,如下所示

https://localhost:8443/obsandbox/connect.html
    #access_token={XXXXX_the_access_token}
    &instance_url=https%3A%2F%2Fna15.salesforce.com
    &id={sf issued id}
    &issued_at=1397587022117
    &signature=giX8JA%2BFqqBlDDsv6jn21Z7c0%2BKvlfvbtUxxsSVVqKs%3D
    &scope=web

然后我向salesforce发出跨域javascript调用,但获取资源和查询调用都不起作用。我已经尝试使用JQuery和Angular同样的结果 - 401 Not Authorized。

顺便说一句,我尝试过使用"授权" :"持票人" +令牌& " OAuth" +令牌。

非常感谢任何见解

拉​​吉

使用JQuery

function sfRes(iElm, tElm) {
var instance = document.getElementById(iElm).value;
var token = document.getElementById(tElm).value;
jQuery.ajax({
    type : "GET",
    dataType : "jsonp",
    url : instance + "/services/data/v29.0/",
    beforeSend : function(request) {
        request.setRequestHeader("Authorization", "OAuth " + token);
        request.setRequestHeader("Content-Type", "application/json");
    },
    crossDomain : true,
    success : function(ret) {
        console.log(ret);
    },
    error : function(jqXHR, textStatus, errorThrown) {
        console.log('Error: ' + jqXHR.status);
        console.log('ErrorThrown: ' + errorThrown);
    }
});
}

function sfQuery(iElm, qElm, tElm) {
var instance = document.getElementById(iElm).value;
var query = document.getElementById(qElm).value;
var token = document.getElementById(tElm).value;
return $.ajax({
    type : "GET",
    dataType : "jsonp",
    url : instance + "/services/data/v29.0/query/",
    data : {
        "q" : query
    },
    beforeSend : function(request) {
        request.setRequestHeader("Authorization", "OAuth " + token);
        request.setRequestHeader("Content-Type", "application/json");
    },
    crossDomain : true,
    success : function(ret) {
        console.log(ret);
    },
    error : function(jqXHR, textStatus, errorThrown) {
        console.log('Error: ' + jqXHR.status);
        console.log('ErrorThrown: ' + errorThrown);
    }
});
}

使用Angular

$scope.sfRes = function(iElm, tElm) {
    var instance = document.getElementById(iElm).value;
    var token = document.getElementById(tElm).value;
    return $http({
        method : "JSONP",
        url : instance + "/services/data/v29.0/",
        headers : {
            "Authorization" : "Bearer " + token,
            "Content-Type" : "application/json"
        }
    }).error(function(data, status, headers, config) {
        return "Failed";
    });
}, 
$scope.sfQuery = function(iElm, qElm, tElm) {
    var instance = document.getElementById(iElm).value;
    var query = document.getElementById(qElm).value;
    var token = document.getElementById(tElm).value;
    return $http({
        method : "JSONP",
        url : instance + "/services/data/v29.0/query/",
        params : {
            "q" : query
        },
        headers : {
            "Authorization" : "Bearer " + token,
            "Content-Type" : "application/json"
        }
    }).then(function(response) {
        return response;
    });

0 个答案:

没有答案