salesforce rest api INVALID_SESSION_ID 错误

发布于 2024-11-09 14:18:24 字数 1069 浏览 0 评论 0原文

我正在使用 salesforcerest api 从我的 Rails 应用程序访问 salesforce 帐户。我创建了一个远程访问应用程序并获取了密钥 N 的 id。我能够对用户进行身份验证并获取 auth_token、实例 url 等等。但是,当我在“instance_url/services/data/v20.0”处发送请求以及访问令牌时,我收到此错误:

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

我有一个开发人员 salesforce 帐户,并且为每个配置文件启用了 API,但“经过身份验证的网站”除外“个人资料(无法访问)。

请问有人可以帮我吗?

我正在使用以下请求对用户进行身份验证,

HTTParty.post "login.salesforce.com/services/oauth2/token";, :body=>{"grant_type"=>"authorization_code","code"=>"abc}","client_secret"=>"abc"‌​, "client_id"=>"abc","format"=>"json","redirect_uri"=>"localhost:3000/salesforce/callback";}

该请求返回签名、id、instance_url、issued_at、access_token 和 refresh_token,

HTTParty.get "ap1.salesforce.com/services/data/v20.0";, :headers=>{"Authentication"=>"OAuth access_token", "Content-Type"=>"application/json"}

其响应为

[{"errorCode"=>"INVALID_SESSION_ID", "message"=>"Session expired or invalid"}]

I am using salesforce rest api to access the salesforce account from my rails app.I created a remote access app and got the key N the id. I was able to authenticate the user and get the auth_token, instance url and all that. But, when I send request at "instance_url/services/data/v20.0" along with the access token , I get this error:

[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

I have a developer salesforce account and have API enabled true for every profile, except for the "Authenticated website" profile(which is not accessible).

Please, can anybody help me with this?

I'm authenticating the user with following request

HTTParty.post "login.salesforce.com/services/oauth2/token";, :body=>{"grant_type"=>"authorization_code","code"=>"abc}","client_secret"=>"abc"‌​, "client_id"=>"abc","format"=>"json","redirect_uri"=>"localhost:3000/salesforce/callback";}

which is returning signature, id, instance_url, issued_at, access_token and refresh_token

HTTParty.get "ap1.salesforce.com/services/data/v20.0";, :headers=>{"Authentication"=>"OAuth access_token", "Content-Type"=>"application/json"}

which responds with

[{"errorCode"=>"INVALID_SESSION_ID", "message"=>"Session expired or invalid"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南渊 2024-11-16 14:18:24

您如何将 sessionId 传递给 /services/data/v20.0 请求?如果您的 access_token 是 abc123 那么您需要一个 Authorization: OAuth abc123 的 http 标头的请求。

How are you passing the sessionId to the /services/data/v20.0 request?, if your access_token is abc123 then you need a http header of Authorization: OAuth abc123 in the request.

万劫不复 2024-11-16 14:18:24

无论是否有活动,Salesforce 中的 API 会话都会过期。
要设置每个会话的持续时间,请转至“设置”>“管理设置>安全控制>会话设置>

最长为 8 小时。

干杯!

API session in salesforce expires regardless if there are activities or not.
to set the duration of each session go to Setup > Administration Setup > Security Controls > Session Settings>

the max is 8hours.

cheers!

青衫负雪 2024-11-16 14:18:24

除了其他答案确定的其他可能问题之外,Salesforce 中的将会话锁定到其发起的 IP 地址 设置可能是导致其他有效代码的一个因素。来自以下 Salesforce 知识库文章

描述
启用“将会话锁定到其来源的 IP 地址”时,如果使用 OAuth2 访问令牌来执行 Salesforce REST API 调用,则即使在同一 Apex 事务中获取令牌,也可能会返回 INVALID_SESSION_ID。

分辨率
“将会话锁定到其来源的 IP 地址”是严格的,在这种情况下,内部 IP 地址不会自动列入白名单。由于登录调出和后续 REST API 调出可能通过不同的内部 IP 地址执行,因此如果启用了上述首选项,则在使用访问令牌时可能会返回 INVALID_SESSION_ID。

要解决此问题,您可以使用连续 IP 强制执行功能(在 Summer '15 中引入):

  1. 关闭“将会话锁定到其发起的 IP 地址”,
  2. 开启“对每个请求强制登录 IP 范围”,
  3. 选择连接的应用的 IP 放宽策略“实施 IP 限制”,然后
  4. 将 Salesforce 的内部 IP 范围 10.0.0.0 到 10.255.255.255 添加到需要使用 Salesforce REST API 的配置文件列表。

或者干脆放宽IP限制:

  1. 关闭“将会话锁定到其发起的 IP 地址”,并且
  2. 选择连接的应用的 IP 放宽政策“放宽 IP 限制”

Additionally to the other possible problems identified by the other answers, the Lock sessions to the IP address from which they originated setting in Salesforce is a possible contributing factor to otherwise valid code. From the following Salesforce KB article:

Description
When "Lock sessions to the IP address from which they originated" is enabled, if an OAuth2 access token is used to perform a Salesforce REST API call, INVALID_SESSION_ID might be returned even if the token is obtained in the same Apex transaction.

Resolution
"Lock sessions to the IP address from which they originated" is strict, and internal IP addresses are not automatically whitelisted in this case. Since the login callout and subsequent REST API callouts might be performed via different internal IP addresses, INVALID_SESSION_ID might be returned when using the access token if the mentioned preference is enabled.

To solve this you may use the continuous IP enforcement feature (introduced in Summer '15):

  1. Turn "Lock sessions to the IP address from which they originated" OFF,
  2. Turn "Enforce login IP ranges on every request" ON,
  3. Select the connected app's IP relaxation policy "Enforce IP restriction", and
  4. Add Salesforce's internal IP range 10.0.0.0 to 10.255.255.255 to the list of profiles needing to use Salesforce's REST API.

or simply relax the IP restrictions:

  1. Turn "Lock sessions to the IP address from which they originated" OFF, and
  2. Select the connected app's IP relaxation policy "Relax IP restrictions"
夕色琉璃 2024-11-16 14:18:24

如果您使用 Oauth,则需要在出现此错误时使用刷新令牌流程来获取新的更新令牌。以下是更多详细信息: http://wiki.developerforce.com/index.php/ Digging_Deeper_into_OAuth_2.0_on_Force.com

请在上面的 WIKI 页面链接中搜索“刷新令牌”。

If you are using Oauth you need to use Refresh Token flow, on this error to get a new renewed token. Here are more details : http://wiki.developerforce.com/index.php/Digging_Deeper_into_OAuth_2.0_on_Force.com

Please search for "refresh token" in the WIKI page link above.

桜花祭 2024-11-16 14:18:24

我遇到的问题:

Issues I ran into:

送舟行 2024-11-16 14:18:24

面临同样的问题,在我的例子中,密码中的 < > 字符导致了问题。

Faced the same issue and in my case < > characters in the password were causing the problem.

离去的眼神 2024-11-16 14:18:24

我添加这个答案是因为上面的任何其他答案都对我有帮助。我的问题实际上是我正在使用 access_token,因为我在 login.salesforce.com/services/oauth2/token 请求期间在 JSON 响应中收到了它。

您必须从 access_token 中删除 ID,如 SalesForce 文档中所述:"用 ID 替换令牌值"

I am adding this answer because any of other answers above helped me. My problem was actually that I was using access_token as I received it in the JSON response during login.salesforce.com/services/oauth2/token requests.

You have to remove ID from access_token, as it is described in SalesForce documentation: "Substitute the ID for the token value"

阪姬 2024-11-16 14:18:24

用户在未连接应用程序的情况下进行连接。您可以尝试在设置选项卡中更改以下设置。设置关注授权标头 - ON

显示设置切换的屏幕截图

Users connecting without connected App. You can try changing below setting in the setting tab. Set Follow Authorization header - ON

Screenshot showing setting toggle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文