salesforce rest api INVALID_SESSION_ID 错误
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您如何将 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 ofAuthorization: OAuth abc123
in the request.无论是否有活动,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!
除了其他答案确定的其他可能问题之外,Salesforce 中的
将会话锁定到其发起的 IP 地址
设置可能是导致其他有效代码的一个因素。来自以下 Salesforce 知识库文章: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:如果您使用 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.
我遇到的问题:
确保在授权请求中包含范围“web”,例如
https://login.salesforce.com/services/oauth2/authorize?response_type=code&clientId=xxx&redirect_uri=http://www.example.com&scope=id +api+refresh_token+web
使用授权响应中返回的instanceUrl。就我而言,这是 https://eu2.salesforce.com 并且我总是尝试使用 https://na1.salesforce.com 不起作用
Issues I ran into:
Make sure to include the scope "web" in the authorize request, e.g.
https://login.salesforce.com/services/oauth2/authorize?response_type=code&clientId=xxx&redirect_uri=http://www.example.com&scope=id+api+refresh_token+web
Use the instanceUrl that is returned in the authorize response. In my case this was https://eu2.salesforce.com and I always tried to use https://na1.salesforce.com which didn't work
面临同样的问题,在我的例子中,密码中的
<
>
字符导致了问题。Faced the same issue and in my case
<
>
characters in the password were causing the problem.我添加这个答案是因为上面的任何其他答案都对我有帮助。我的问题实际上是我正在使用 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"
用户在未连接应用程序的情况下进行连接。您可以尝试在设置选项卡中更改以下设置。设置关注授权标头 - ON
Users connecting without connected App. You can try changing below setting in the setting tab. Set Follow Authorization header - ON