我試圖將我的asp.net REST api連接到salesforce。我成功地通過認證,然而當我開始發送POST請求,我得到一個錯誤Salesforce休息api INVALID_SESSION_ID
{「錯誤碼」:「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響應。 此外,我試圖從簡單的休息客戶端發送一個POST請求與相同的令牌,它得到的200響應。我試圖將我的「授權:持票人」標題更改爲「授權:Oauth」,但沒有任何更改。我也嘗試捕獲這個錯誤,獲取刷新標記並使用刷新的標記再次發送請求,但沒有任何更改。請在這件事上給予我幫助。
您是否嘗試攔截其餘api調用您的代碼正在向salesforce進行調用。你有沒有看到通過你的代碼完成的API調用的實際內容?它看起來像你的cookie的問題。標記不會保存在您的Cookie中,或者您沒有以正確的格式發送它。 – 2014-09-06 13:14:40