Micro-Credit Public account verification method

Source: Internet
Author: User
Tags php file svn in python

Some time ago I applied for a micro-credit public account, wanted to implement automatic response on the server side, and also contacted about Django programming to continue my web path in Python. Finally, I have time to record this process, as follows:

First clear goal-to become the micro-public number of Third-party developers, through the message API certification.

Languages and frames to use: Python Django

Platform of the host: SAE Cloud Platform

Micro-trust platform: micro-trust public account

Of course, first read the micro-letter message interface, it is through the micro-mail server to get the user's reply, with GET request sent to my server, and then I give a response. So you just need simple programming to do the validation.

Step 1: Apply for a micro-credit public account, you need to fill out a number of information, transfer door http://mp.weixin.qq.com, fill out, you can read its open API document, which mentions how to verify: Send a Echochar, the original return on it. However, to verify that the GET request is from a micro-server, you need to check the generated hash value in detail. The document contains a sample PHP file that can be downloaded and read directly.

Step 2: Build the application on the SAE, have the account number first, then create a new app, choose Python or PHP, if you choose PHP can use the code provided by the micro-letter, Google will also have a very detailed tutorial. If you choose Python, you can also refer to this article or the link behind the article.

On the SAE to build applications, you need to create a local version of the library, so the SAE management platform to initialize the first version, this version of what no code, only two profiles Config.yaml,index.wsgi, with SVN check out to the local working directory. You can see a folder named "1", which is the first version. Then we can build Django apps in that folder, and if you have Django installed, you can go to directory "1" and run it on the command line.

django-admin.py Startproject MySite

MySite is the name of your application and will be replaced when you run the command.

The local working directory is then created, but the directory to create is below 1, and we end up with the following: Tree command view directory trees

So move the files or directories under MySite to 1, because mysite/is temporarily not in version control, so you can manually copy the files or directories into the SVN directory with SVN. Thus forming manage.py under 1, the other Django files are under MySite (here is Scnuwriter).

Step 3: Set up the Django application locally and upload it to the SAE server with the SVN commit command. Here you need to refer to the SAE documentation for an operation to build the Python development environment. If you have questions, you can see here. In short, to configure all kinds of files, upload code on the line.

Step 4: Write the code for the micro-authentication code, the following view.py file:

#-*-Coding:utf-8-*-     
from django.http import HttpResponse     
to django.template import RequestContext, template     
from DJANGO.VIEWS.DECORATORS.CSRF import csrf_exempt     
from django.utils.encoding import Smart_str, Smart_ Unicode    
import hashlib   


TOKEN = "MyToken"

# to verify API server             
def checksignature (request):     
    Global TOKEN     
    signature = Request. Get.get ("signature", None)   
    timestamp = Request. Get.get ("timestamp", None)   
    nonce = Request. Get.get ("Nonce", None)   
    echostr = Request. Get.get ("Echostr", None)   
    token = token     
    tmplist = [Token,timestamp,nonce]     
    tmplist.sort ()     
    tmpstr = "%s%s%s"% tuple (tmplist)     
    tmpstr = HASHLIB.SHA1 (tmpstr). Hexdigest ()   
    if tmpstr = = Signature: Return    
        HttpResponse (ECHOSTR)   
    else: return     
        None

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.