如何使用 Zoho CRM v2 API 添加 Zoho CRM 潜在客户 [英] How To Add Zoho CRM Leads with the Zoho CRM v2 API

查看:20
本文介绍了如何使用 Zoho CRM v2 API 添加 Zoho CRM 潜在客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用简单的 curl 连接通过 Zoho CRM v2 API 添加 Zoho CRM 线索?文档对如何添加潜在客户以及如何获取正确的 oAuth 令牌来创建潜在客户都不是很清楚.

解决方案

文档 对这个过程不是很清楚.所以,我会简化这个.最大的障碍是获取刷新令牌的步骤.一旦你有了它,你就可以生成无数的访问令牌,用于进行实际的 API 调用.

如何获取刷新令牌

  1. 使用管理员级别帐户登录您的 Zoho CRM,然后在选项卡中打开该帐户.然后,在另一个浏览器选项卡中,登录开发者 API 控制台:https://api-console.zoho.com/
  2. 在开发者 API 控制台中,您需要创建一个服务器端客户端.这些字段应按如下方式填充:客户名称(您想要的任何逻辑名称)、主页网址(您将在其中制作 API 的域的主页调用,或者只是使用相同的 URL 作为您进行重定向的位置),授权的重定向 URI(在您要进行 API 调用的域上放置一个网页并使其发出的只是zoho 确认").(zoho 确认"不是必需的,文档中也没有提及,但确实可以帮助您更好地理解该过程.)点击创建.
  3. 创建后,再次单击它,就会出现一个名为 Client Secret 的选项卡.单击它并在记事本中记录您的客户 ID客户机密.
  4. 在线获取 URL 编码器脚本(或使用 PHP 或任何其他工具),然后对以下参数进行 URL 编码,以获取您需要在下一步中编写和加载到浏览器中的新 URL:

范围:ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,ZohoCRM.users.ALL,ZohoCRM.org.ALL,aaaserver.profile.ALL,ZohoCRM.settings.functions.all,ZohoCRM.notifications.全部,ZohoCRM.coql.read,ZohoCRM.files.create,ZohoCRM.bulk.allresponse_type: 代码access_type:离线client_id:{使用您在第 3 步中记录的客户 ID}redirect_uri:{使用您在第 2 步中生成的授权重定向 URI}

  1. 使用这些 URL 编码参数,将它们传递到浏览器中此 URL 的末尾:https://accounts.zoho.com/oauth/v2/auth?类似于:

https://accounts.zoho.com/oauth/v2/auth?scope=ZohoCRM.modules.ALL%2CZohoCRM.settings.ALL%2CZohoCRM.users.ALL%2CZohoCRM.org.ALL%2Caaaserver.profile.ALL%2CZohoCRM.settings.functions.all%2CZohoCRM.notifications.all%2CZohoCRM.coql.read%2CZohoCRM.files.create%2CZohoCRM.bulk.all&client_id=1000.TTT0BSTTTC9TTTRILR3MGXBY&H&H&line&client_id;redirect_uri=https%3A%2F%2Fexample.com%3Azohoconfirm

请注意,如果您的重定向 URI(例如 https://example.com/zohoconfirm,上面) 执行另一个重定向,例如 301 或 302 重定向,并且不转发这些查询参数,然后您将丢失完成后需要查看的参数.

  1. 在浏览器中加载后,您将看到一个接受按钮.如果不这样做,那么您可能错过了在第 1 步中您应该在一个选项卡中登录 CRM,然后打开一个新选项卡来执行其他任务的操作.这会为您打开一个会话,并且在执行第 5 步时它知道您是谁.

  2. 在您单击接受按钮后,Zoho 的帐户系统会将您重定向到您指定的授权重定向 URI.因此,如果您完全按照这些步骤操作,您应该会看到一些类似zoho 确认"的输出.但不要关闭选项卡.查看选项卡的 URL 并将其剪切/粘贴到记事本中.在那里,获取代码参数.

  3. 既然您有了那个代码参数,您需要创建一个特殊的 Curl POST 请求.您可以通过命令行或使用您喜欢的编程语言来执行此操作.从 Linux 上的命令行,这将类似于以下内容:

curl "https://accounts.zoho.com/oauth/v2/token" -X POST -d grant_type = authorization_code&安培;代码= 1000.aaa1f9aa8582eaaaafeaddfaf0f3b6b.bf7fe646ba899783501c21a9a3240aaa&安培; CLIENT_ID = 1000.AAAA0BSIC7C9AAAARILR3MGXBYAC8AAA&安培; client_secret = aaa9aa9166055932ff8c0279c6253a65cb534daaaa&安培; REDIRECT_URI = HTTPS%3A%2F%2Fexample.com%2Fzohoconfirm"

您需要填写的网址编码参数为:

grant_type:authorization_code代码:{您在第 7 步中记录的代码}client_id:{您在第 3 步中记录的客户 ID}client_secret:{您在第 3 步中记录的客户端密钥}redirect_uri:{您在第 2 步中记录的授权重定向 URI}

  1. 运行该 Curl 命令后,它将返回一些类似于以下内容的 JSON:

 <代码> { ACCESS_TOKEN": 1000.aaaa5579abeea73871b096c941ec1df8.7835dbb43bbf5122f2d5810f0b65aaa", refresh_token": 1000.aaaa2683a1723d1aeb7b58899849aaaa.aaaa4c22481da67e85e598f4b988aaa", api_domain": https://www.zohoapis.com", token_type":"Bearer","expires_in":3600}

  1. 现在,在 JSON 输出中,您可以使用该 access_token 进行 API 调用以生成潜在客户,但您最好只使用 >refresh_token 并将其放入配置文件中,以便以后在您的编程中使用.根据他们的文档,他们的 refresh_token 永远存在.你应该把它放在一个非常安全的地方.

如何获取您的访问令牌

  1. 现在您获得了refresh_token,您可以在每次需要进行 API 调用时使用它来生成一个新的access_token.为此,请使用类似于以下的 URL 进行另一个 Curl POST.例如,在带有 Curl 的 Linux 命令行中,它的外观如下所示:

curl "https://accounts.zoho.com/oauth/v2/token" -X POST -d grant_type = refresh_token&安培; refresh_token = 1000.aaaa2683a1723d1aeb7b58899849aaaa.aaaa4c22481da67e85e598f4b988aaa&安培; CLIENT_ID = 1000.AAAA0BSIC7C9AAAARILR3MGXBYAC8AAA&安培; client_secret = aaa9aa9166055932ff8c0279c6253a65cb534daaaa"

这里解释了如何做 URL 编码的参数:

grant_type: refresh_tokenrefresh_token:{使用前面步骤中第 10 步中存储的配置文件中的 refresh_token(我说过会永远持续的那个)}client_id:{使用您的 client_id}client_secret: {使用你的 client_secret}

  1. 运行上述命令应该会返回一些类似于以下内容的 JSON:

{"access_token":"1000.aaa1c95788b34226c2844a841bfd1d5.6aff70f4adbc3fee9e6b57579e481aaa","api_domain":"https://www.zohotoken","34226c2844a841bfd1d5.6aff70f4adbc3fee9e6b57579e481aaa":"https://www"type","34226c2844a841bfd1d5.6

  1. 使用您最喜欢的编程语言,您将收到的 access_token 存储在一个临时变量中,以便您可以进行 API 调用以执行诸如添加 Zoho CRM 潜在客户之类的操作.该访问令牌将持续 3600 秒(1 小时),但实际上无关紧要,因为您可以生成无限的访问令牌,因为您将 refresh_token 存储在配置文件中.

如何创建 Zoho CRM 潜在客户

  1. 在您最喜欢的编程语言中,您将使用该刷新令牌来创建访问令牌,然后将其用于以下标头以传递您的 API 调用:

授权:Zoho-oauthtoken XXXXXX

...其中 XXXXXX 是您从上一组指令中收到的 access_token.

  1. 这是一个 PHP 示例,使用 file_get_contents(尽管您可以使用 PHP 的 Curl 执行相同操作)以请求添加 Zoho CRM 线索.您可以采用以下示例并使用您喜欢的编程语言对其进行重新编码.将 $sAccessToken 值更改为您生成的值.

注意,在下面,我们还将写入一个名为Lead Source"的字段,它的 API 可访问名称是Lead_Source".(如果您最终更改下面的代码以写入联系人或客户,那么您将需要创建一个自定义字段,我敢肯定,以指示联系人或客户的来源.例如,如果您有一个购物购物车,然后您可以表明记录来自完成该交易,而不是客户来电.)

'米奇','姓氏' =>'老鼠','电子邮件' =>'mickey@example.com','电话' =>'444-444-4444','Lead_Source' =>'测试1'));$sJSON = str_replace('{','[{',$sJSON);$sJSON = str_replace('}','}]',$sJSON);$sJSON = '{"data":' .$sJSON .'}';echo "发送:$sJSON
";$sURL = 'https://www.zohoapis.com/crm/v2/Leads';$sResponse = @file_get_contents($sURL,false,stream_context_create(array('http'=>array('ignore_errors' =>TRUE,//如果您想在响应中看到错误而不是在错误时为空,则至关重要'方法' =>'邮政','标题' =>大批('内容类型:应用程序/json',"授权:Zoho-oauthtoken $sAccessToken",'缓存控制:无缓存'),'内容' =>$sJSON))));echo "$sResponse
";

如果成功,这将生成类似于以下的响应:

{"data":[{"code":"SUCCESS","details":{"Modified_Time":"2020-05-21T20:45:24-04:00","Modified_By":{"name":"John Manager","id":"9999889000000279991"},"Created_Time":"2020-05-21T20:45:24-04:00","id":"999988900000027"Created_By":{"name":"John Manager","id":"9999889000000279991"}},"message":"记录添加","status":"成功"}]}

...其中 John Manager 是您登录 CRM 所用的管理员级别用户帐户的名称.

可在此处找到有关 Zoho CRM 插入记录 API 的更多信息:https://www.zoho.com/crm/developer/docs/api/insert-records.html

要了解在您的 CRM 中在哪里可以找到 API 可寻址字段名称,请转到:设置 > 开发人员空间 > API > API 名称 > 潜在客户.从过滤依据"下拉菜单中选择字段".

请注意,此 API 不会查找重复项.您可以修改上述内容以执行更新插入"而不是插入":https://www.zoho.com/crm/developer/docs/api/upsert-records.html

如果您编辑联系人和客户以具有与潜在客户相同的自定义字段,那么您可以将 https://www.zohoapis.com/crm/v2/Leads 行更改为 https://www.zohoapis.com/crm/v2/Contactshttps://www.zohoapis.com/crm/v2/Accounts 并将条目写入其中.

How do I add a Zoho CRM Lead with the Zoho CRM v2 API, using simple curl connections? The docs are not very clear on not only how to add a lead, but how to even get the proper oAuth token to create the lead.

解决方案

The docs are not very clear on this process. So, I'll simplify this. The biggest hurdle are the steps to get the refresh token. Once you have that, you can generate countless access tokens that are used to make the actual API calls.

How To Get Your Refresh Token

  1. Login to your Zoho CRM with an admin-level account and have that open in a tab. Then, in another browser tab, login into the Developer API Console: https://api-console.zoho.com/
  2. In the Developer API Console, you need to create a Server-Side Client. The fields should be populated like so: Client Name (any logical name you want), Homepage URL (the homepage of the domain where you're going to be making the API calls, or just use the same URL for where you're doing the redirects), Authorized Redirect URIs (put a web page up on the domain where you're going to make the API call and make it emit nothing more than "zoho confirmed"). (The "zoho confirmed" is not required and not mentioned in the docs, but does help you understand the process better.) Click Create.
  3. Once created, click on it again and there's a tab called Client Secret. Click it and record in a notepad your Client ID and Client Secret.
  4. Take a URL encoder script online (or use PHP or any other tool) and URL encode the following parameters for a new URL you need to compose and load in your browser in the next step:

scope: ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,ZohoCRM.users.ALL,ZohoCRM.org.ALL,aaaserver.profile.ALL,ZohoCRM.settings.functions.all,ZohoCRM.notifications.all,ZohoCRM.coql.read,ZohoCRM.files.create,ZohoCRM.bulk.all
response_type: code
access_type: offline
client_id: {use the Client ID you recorded in step 3}
redirect_uri: {use the Authorized Redirect URI you generated in step 2}

  1. With those URL encoded parameters, pass them to the end of this URL in your browser: https://accounts.zoho.com/oauth/v2/auth? Thus, you would have something similar to:

https://accounts.zoho.com/oauth/v2/auth?scope=ZohoCRM.modules.ALL%2CZohoCRM.settings.ALL%2CZohoCRM.users.ALL%2CZohoCRM.org.ALL%2Caaaserver.profile.ALL%2CZohoCRM.settings.functions.all%2CZohoCRM.notifications.all%2CZohoCRM.coql.read%2CZohoCRM.files.create%2CZohoCRM.bulk.all&client_id=1000.TTT0BSTTTC9TTTRILR3MGXBYAC82LH&response_type=code&access_type=offline&redirect_uri=https%3A%2F%2Fexample.com%3Azohoconfirm

Please note that if your redirect URI (like https://example.com/zohoconfirm, above) does another redirection with like a 301 or 302 redirect and doesn't also forward those query parameters, then you'll lose the parameters you need to see when finished.

  1. Once you load that in your browser, you will see an Accept button. If you do not, then you may have missed that in step 1 you were supposed to login to the CRM in one tab, and then open a new tab to do other tasks. That makes a session open for you and it knows who you are when doing step 5.

  2. After you click the Accept button, Zoho's account system will redirect you to your Authorized Redirect URI that you specified. So, if you followed these steps exactly, you should see some output like "zoho confirmed". But do not close the tab. Look at the URL of the tab and cut/paste it into your notepad. In there, grab that code parameter.

  3. Now that you have that code parameter, you need to create a special Curl POST request. You can do this either via command line or with your favorite programming language. From command line on Linux, this would look something like the following:

curl "https://accounts.zoho.com/oauth/v2/token" 
-X POST 
-d "grant_type=authorization_code&code=1000.aaa1f9aa8582eaaaafeaddfaf0f3b6b.bf7fe646ba899783501c21a9a3240aaa&client_id=1000.AAAA0BSIC7C9AAAARILR3MGXBYAC8AAA&client_secret=aaa9aa9166055932ff8c0279c6253a65cb534daaaa&redirect_uri=https%3A%2F%2Fexample.com%2Fzohoconfirm"

The URL-encoded parameters you need to fill out are:

grant_type: authorization_code
code: {the code you recorded from step 7}
client_id: {the Client ID you recorded in step 3}
client_secret: {the Client Secret you recorded in step 3}
redirect_uri: {the Authorized Redirect URI you recorded in step 2}

  1. Once you run that Curl command, it will return some JSON that looks similar to the following:

{"access_token":"1000.aaaa5579abeea73871b096c941ec1df8.7835dbb43bbf5122f2d5810f0b65aaa","refresh_token":"1000.aaaa2683a1723d1aeb7b58899849aaaa.aaaa4c22481da67e85e598f4b988aaa","api_domain":"https://www.zohoapis.com","token_type":"Bearer","expires_in":3600}

  1. Now, in the JSON output, you could use that access_token to make an API call to generate the lead, but you're better off just taking that refresh_token and putting that in a configuration file for later use in your programming. According to their documentation, their refresh_token lasts forever. You should keep this in an extremely secure place.

How To Get Your Access Token

  1. Now that you get your refresh_token, you use it to generate a fresh access_token each time you need to make an API call. To do that, make another Curl POST with a URL similar to below. In Linux command line with Curl, as an example, here's what it would look like:

curl "https://accounts.zoho.com/oauth/v2/token" 
-X POST 
-d "grant_type=refresh_token&refresh_token=1000.aaaa2683a1723d1aeb7b58899849aaaa.aaaa4c22481da67e85e598f4b988aaa&client_id=1000.AAAA0BSIC7C9AAAARILR3MGXBYAC8AAA&client_secret=aaa9aa9166055932ff8c0279c6253a65cb534daaaa"

Here's an explanation of how to do the URL-encoded parameters:

grant_type: refresh_token
refresh_token: {use the refresh_token (the one I said that would last forever) from your stored config file from step 10 in the previous steps}
client_id: {use your client_id}
client_secret: {use your client_secret} 

  1. Running the above command should return to you some JSON that looks similar to:

{"access_token":"1000.aaa1c95788b34226c2844a841bfd1d5.6aff70f4adbc3fee9e6b57579e481aaa","api_domain":"https://www.zohoapis.com","token_type":"Bearer","expires_in":3600}

  1. In your favorite programming language, you'll be storing that access_token you received in a temporary variable so that you can make an API call to do something like add a Zoho CRM Lead. That access token will last 3600 seconds (1 hour), but really is of no consequence because you can generate infinite access tokens since you have that refresh_token stored in a configuration file.

How To Create A Zoho CRM Lead

  1. In your favorite programming language, you'll be using that refresh token to create an access token, and then using it for the following header to pass in your API call:

Authorization: Zoho-oauthtoken XXXXXX

...where XXXXXX is the access_token you received from the previous set of instructions.

  1. Here's a sample in PHP with file_get_contents (although you can do the same with PHP's Curl) to make a request to add a Zoho CRM Lead. You can take the following example and recode it in your favorite programming language. Change the $sAccessToken value to the one you generated.

Note, below, we're also writing into a field called "Lead Source", and it's API-accessible name is "Lead_Source". (If you end up changing the code below to write into Contacts or Accounts, then you'll want to create a custom field, I'm sure, to indicate where the contact or account came from. For instance, if you have a shopping cart, then you could indicate the record came from completing that transaction, rather than a customer calling in.)

<?php

error_reporting(E_ALL);
ini_set('display_errors','On');
header('Content-Type: text/plain');

$sAccessToken = '1000.aaaa5579abeea73871b096c941ec1df8.7835daaa3bbf5122f2d5810f0b65aaaa'; // change me
$sJSON = json_encode(array(
    'First_Name' => 'Mickey',
    'Last_Name' => 'Mouse',
    'Email' => 'mickey@example.com',
    'Phone' => '444-444-4444',
    'Lead_Source' => 'TEST1'
  ));
  $sJSON = str_replace('{','[{',$sJSON);
  $sJSON = str_replace('}','}]',$sJSON);
  $sJSON = '{"data":' . $sJSON . '}';
echo "SENDING: $sJSON
";
$sURL = 'https://www.zohoapis.com/crm/v2/Leads';
$sResponse = @ file_get_contents($sURL,false,stream_context_create(array('http'=>array(
  'ignore_errors' => TRUE, // critical if you want to see errors in response instead of empty on error
  'method' => 'POST',
  'header' => array(
    'Content-Type: application/json',
    "Authorization: Zoho-oauthtoken $sAccessToken",
    'cache-control: no-cache'
  ),
  'content' => $sJSON
))));
echo "$sResponse
";

If successful, this will generate a response similar to below:

{"data":[{"code":"SUCCESS","details":{"Modified_Time":"2020-05-21T20:45:24-04:00","Modified_By":{"name":"John Manager","id":"9999889000000279991"},"Created_Time":"2020-05-21T20:45:24-04:00","id":"9999889000000279991","Created_By":{"name":"John Manager","id":"9999889000000279991"}},"message":"record added","status":"success"}]}

...where John Manager would be the name of the admin-level user account you logged into the CRM with.

More information about the Zoho CRM Insert Record API can be found here: https://www.zoho.com/crm/developer/docs/api/insert-records.html

To understand where to find the API-addressable field names, in your CRM, go to: Setup > Developer Space > APIs > API Names > Leads. Choose "Fields" from the "Filter By" drop-down.

Note that this API does not look for duplicates. You can adapt the above to do an "upsert" instead of an "insert": https://www.zoho.com/crm/developer/docs/api/upsert-records.html

If you edit the Contacts and Accounts to have the same custom fields as Leads, then you can change the https://www.zohoapis.com/crm/v2/Leads line to https://www.zohoapis.com/crm/v2/Contacts or https://www.zohoapis.com/crm/v2/Accounts and write entries into those as well.

这篇关于如何使用 Zoho CRM v2 API 添加 Zoho CRM 潜在客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆