具有自动登录功能的Salesforce php REST API

时间:2014-02-20 10:51:56

标签: php api rest authentication salesforce

我现在正把头发拖过这一头。我遵循了以下示例:

http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php

但是这里用户被发送到登录表单并需要登录。相反,我想在我的代码中发布该数据,而不是让用户登录,但我的应用程序会自动执行此操作。

如果有人可以发布一个关于如何使用oAuth执行此操作的示例,我将非常感激,因为我并不急于使用这种臃肿的SOAP实现。

干杯!

2 个答案:

答案 0 :(得分:12)

似乎经过一些修补我的尝试取得了成功::

$loginurl = "https://login.salesforce.com/services/oauth2/token";

$params = "grant_type=password"
. "&client_id=" . CLIENT_ID
. "&client_secret=" . CLIENT_SECRET
. "&username=" . USER_NAME
. "&password=" . PASSWORD;

$curl = curl_init($loginurl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
    die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}

curl_close($curl);

echo $json_response;

现在剩下要做的就是存储 access_token& instance_url从该响应变为会话变量 并消除我们的对象。

我希望以上帮助其他有类似问题的人。

答案 1 :(得分:0)

2018答案:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm

使用以下命令发出POST请求:

  • grant_type:此身份验证流程必须为password
  • client_id:来自已连接应用定义的消费者密钥。
  • client_secret:来自连接的应用定义的消费者秘密。除非在连接的应用程序定义中未启用“Web服务器流的要求密钥”设置,否则必需。
  • username:最终用户的用户名。
  • password:最终用户的密码。

示例:

grant_type=password&client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82Hn FVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&client_secret= 1955279925675241571&username=testuser%40salesforce.com&password=mypassword123456

有关详细信息,请参阅上面的链接。