当前请求的路由是从localhost:3001发起的,经过同一台主机上运行的localhost:3001/proxy代理,然后请求被路由到Salesforce实例。代理使用ExpressJS制作,客户端应用程序使用AngularJS制作。注意:我确保在我的密码末尾标记了安全令牌(Salesforce API要求),但是当使用cURL时,似乎不必这样做。以下是一系列HTTP跟踪,希望能提供一些线索:
来自Angular.JS应用程序的HTTP请求日志:
如您所见,我从cURL请求中收到了有效的响应。 我怀疑代理存在问题,因为可能没有将所有表单数据转发到Salesforce,但我不知道如何在Express.JS中进行调试。 我怀疑的原因是,如果我尝试
POST /proxy HTTP/1.1
Host: localhost:3001
Connection: keep-alive
Content-Length: 234
Pragma: no-cache
Cache-Control: no-cache
X-User-Agent: salesforce-toolkit-rest-javascript/v29.0
Origin: http://localhost:3001
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/plain, */*
SalesforceProxy-Endpoint: https://uniquename.salesforce.com/services/oauth2/token
Referer: http://localhost:3001/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: liveagent_oref=; liveagent_ptid=3c69c2f9-139d-4439-ba6c-fd8d9dcae101; liveagent_vc=5
grant_type=password&client_id=3MVGxyzxyzxyzxyz&client_secret=99889988&username=first.last%40email.com&password=pswdwACYodaYfHs
400 Bad Request
Object {error_description: "grant type not supported", error: "unsupported_grant_type"}
用于代理路由的相关Express.JS代码:
app.all('/proxy', function(req, res) {
var url = req.header('SalesforceProxy-Endpoint');
console.log(req.body); //prints all form data variables in JSON format
console.log(res.body); //undefined
request({url: url}).pipe(res).on('error', function(error){
//I think I may need to pipe more information using request?
console.log(error);
});
});
使用cURL获取请求的详细信息:
curl -v https://uniquename.salesforce.com/services/oauth2/token
-d "grant_type=password" -d "client_id=3MVGxyzxyzxyzxyz"
-d "client_secret=99889988" -d "username=jfirst.last@email.com" -d "password=pswd"
> POST /services/oauth2/token HTTP/1.1
> User-Agent: curl/7.41.0
> Host: uniquename.salesforce.com
> Accept: */*
> Content-Length: 207
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 207 out of 207 bytes
< HTTP/1.1 200 OK
< Date: Wed, 29 Jul 2015 06:04:55 GMT
< Set-Cookie: BrowserId=auu1mgvHSMS1EedDEduz8Q;Path=/;Domain=.salesforce.com;Exp
ires=Sun, 27-Sep-2015 06:04:55 GMT
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Pragma: no-cache
< Cache-Control: no-cache, no-store
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
<
{
"id":"https://test.salesforce.com/id/05390530530",
"issued_at":"1438132525896197",
"token_type":"Bearer",
"instance_url":"https://uniquename.salesforce.com",
"signature":"blahblah",
"access_token":"XXXXXXXXXXXXXXXXX"
}
* Connection #0 to
host uniquename.salesforce.com left intact
如您所见,我从cURL请求中收到了有效的响应。 我怀疑代理存在问题,因为可能没有将所有表单数据转发到Salesforce,但我不知道如何在Express.JS中进行调试。 我怀疑的原因是,如果我尝试
curl https://uniquename.salesforce.com/services/oauth2/token
它会返回相同的unsupported_grant_type
错误。
request.post(url).form({key:'value'})
方法,这是提交URL编码表单的方式,但我仍然收到400响应。 - jlewkovichrequest({url: url, headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
但它仍然不起作用...我应该以另一种方式传递它吗? - jlewkovich