Home  >  Article  >  WeChat Applet  >  Force.com WeChat development series background configuration

Force.com WeChat development series background configuration

高洛峰
高洛峰Original
2017-02-25 17:05:141536browse

In order to find domestic free cloud resources as the WeChat backend, I spent a day trying out SinaAppEngine (SAE). The debugging was too inconvenient and the user experience was poor. As a media company, Sina’s technical skills cannot stand the test. Amazon can launch AWS, but Sina cannot! A better option is Baidu BaiduAppEngine (BAE), but even though it has recently started charging, it is better to return to force.com with peace of mind. At least the free stuff promised by foreigners will always be free and reliable.


The first step is to apply for a force.com account. Please go to developer.force.com to apply. If you apply for www.salesforce.com, it will be a customer account, not Suitable for developers; the second step is to create an Apex Rest Class as the WeChat service interface. The class created here is WeChatRestController, and the Rest interface name is WeChatRest:

@RestResource(urlMapping='/WeChatRest/*')
global with sharing class WeChatRestController{
@HttpGet
    global static void doGet(){
        String signature = RestContext.request.params.get('signature');
        String timestamp = RestContext.request.params.get('timestamp');
        String nonce = RestContext.request.params.get('nonce');
        String echostr = RestContext.request.params.get('echostr');
        System.debug(echostr + ' - ' + timestamp);
        RestContext.response.addHeader('Content-Type', 'text/plain');
         
        RestContext.response.responseBody = Blob.valueOf(echostr);
         
        //return echostr;
    }
}

We will use this interface to configure in WeChat URL, Tencent requires this URL during configuration. The configuration window is as follows:

Additional explanation: After this article was published, Tencent launched the "Message Encryption and Decryption Key", which supports plain text mode, compatibility mode and security mode. , the main difference is that in plain text mode, Tencent pushes plain text XML messages to the interface, in compatibility mode, there are both plain text XML messages and encrypted XML messages, and in safe mode, only encrypted XML messages are provided to provide users with more advanced Security support simplifies everyone’s learning curve. This article and all subsequent related articles use plain text mode.

Force.com WeChat development series background configuration

Tencent will send four parameters to this URL through the Get method. The URL format is as follows (example only): https://msd-developer-edition.ap1.force. com/services/apexrest/WeChatRest?signature=GenePoint&echostr=test×tamp=111&nonce=222 If the value returned by the URL is consistent with the echostr value in the four parameters, Tencent will consider the verification successful. If they are inconsistent, the verification will be considered failed and the configuration cannot continue. Lazy The method is to directly return the value. The risk is that your URL can also be used by others. The value of echostr here is actually calculated based on the Token configured by the user in the Tencent backend plus a timestamp, plus the value of the nonce parameter, according to a certain algorithm. Please refer to the notes for the specific algorithm and will not go into details here. What needs to be explained in the code is that if echostr is returned directly, force.com will be the content of an xml structure by default. Tencent will think that it is not equal to the echostr value, and it will be judged as a configuration failure. To do this, you need to use the Blog.valueOf method to convert it directly into text information. The third step is to configure Site.com to enable public network access to this URL. The force.com class and web pages require user authentication by default before they can be accessed. However, we can enable public network access to this URL through site.com. To do this, we first need to create Domain name, enter Develop->Sites, enter the domain name you want to create, for example, the author’s domain name is johnson0001:

http://johnson0001-developer-edition.ap1.force.com

After the creation is successful, the next step is to create the site, click the New button:

Force.com WeChat development series background configuration

#Enter Site label, Site Name in the next site information input screen, and select any page as " "Active Site Home Page" (no need to create a web page here, just choose any), keep other options unchanged, click the "save" button:

Force.com WeChat development series background configuration

In the next step Click the "Public Access Settings" button on the screen,

Force.com WeChat development series background configuration

# Find "Enable Apex Class Access" on the next screen, and click the Edit button

Force.com WeChat development series background configuration

Add the WeChatRestController class to the list on the right, which will enable the class to support anonymous public access:

Force.com WeChat development series background configuration

##Finally, return to the Sites homepage and click " Activate" button to activate the site. At this time, you can access the interface we developed through the following URL (note that it may take a few minutes for the URL to take effect):

https://johnson0001-developer-edition.ap1.force .com/services/apexrest/WeChatRest


The fourth step is to configure the Tencent backend. In development mode, enter this URL, enter any Token, and click the submit button. If Tencent prompts success, it means success. Complete configuration:

Force.com WeChat development series background configuration

Verify URL Echostr algorithm: 1. Combine Token (the value configured by the user in the Tencent backend), timestamp (the timestamp value passed when Tencent requests the URL), nonce (Tencent requests the URL) The nonce value passed in) is arranged in alphabetical order; 2. After arrangement, it is spliced ​​into a string; 3. The result of converting this string through the sha1 algorithm is the value of echostr if it is normal

More For articles related to the background configuration of the Force.com WeChat development series, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn