我正在尝试将我的asp.net REST api连接到salesforce。我已成功通过身份验证,但当我开始发送POST请求时,我收到错误
{“errorCode”:“INVALID_SESSION_ID”,“消息”:“会话已过期或无效”}
这是我的POST请求:
//SFServerUrl = "https://na17.salesforce.com/services/";
//url = "data/v28.0/sobjects/Account";
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postBody);
HttpWebRequest request = WebRequest.Create(Globals.SFServerUrl + url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = postBytes.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
HttpCookie cookie = HttpContext.Current.Request.Cookies[Globals.SFCookie];
var ticket = FormsAuthentication.Decrypt(cookie.Value);
string authToken = ticket.UserData;
request.Headers.Add("Authorization", "Bearer " + authToken);
postStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
return new Tuple<bool, string>(true, sb.ToString());
当我尝试发送GET请求时 - 我收到200回复。 此外,我尝试从Simple Rest Client发送一个带有相同令牌的POST请求,并获得200响应。我试图将我的“授权:持票人”标题更改为“授权:Oauth”,但没有任何改变。我还尝试捕获此错误,获取刷新令牌并使用刷新的令牌再次发送请求,但没有任何更改。请帮帮我。
答案 0 :(得分:1)
使用workbench我能够将以下JSON发布到/services/data/v29.0/sobjects/Account
并创建一个新帐户。
{
"Name" : "Express Logistics and Transport"
}
原始回复
HTTP / 1.1 201创建
日期:太阳,2014年9月7日21:32:06 GMT
Set-Cookie:BrowserId = _HC-bzpTQABC1237vFu2hA; Path = /; Domain = .salesforce.com; Expires = Thu,06-Nov-2014 21:32:06 GMT
到期日:1970年1月1日星期四00:00:00 GMT
Sforce-Limit-Info:api-usage = 209/15000
位置:/services/data/v29.0/sobjects/Account/0010000000000001AAA
Content-Type:application / json; charset = UTF-8
内容编码:gzip
Transfer-Encoding:chunked{
&#34; ID&#34; :&#34; 0010000000000001AAA&#34;,
&#34;成功&#34; :真的,
&#34;错误&#34; :[]
}
要检查的事项:
/services
SFServerUrl
是否与发布会话ID的Salesforce pod /服务器相同?如果会话ID来自另一个pod,那么它将在na17上无效。顺便提一下,Salesforce StackExchange网站是询问Salesforce特定问题的好地方。
另见:
答案 1 :(得分:1)
好吧,也许它有点晚了但无论如何。问题是我在内容后添加了标题。当我切换这些代码行时 - 一切正常:)