I have been paying attention to leancloud for some time, and its announced support for multiple development languages, as well as services such as SMS, object storage, user management and authentication, push and chat, are very attractive for developing mainstream mobile applications. Different mobile applications we develop need to maintain different management backgrounds and services, which is repetitive and cumbersome to develop. If leancloud can help us reduce these tasks, it is really worth it.
In order to migrate the application to leancloud, the scheme is as follows:
Use leancloud as the back-end storage, user management and authentication engine;
Use python development management interface to help administrators edit articles and publish information;
Using JS and OC as front-end languages to develop user interface;
In the second step, we want to try the following two simple user cases:
Create several objects and store them in leancloud;
Register users, determine the registration results, and verify the provided mobile phone and email address;
These are the two simplest use cases, which were not solved overnight. First of all, apart from official documents, leancloud can hardly see any other forms of documents, such as FAQ, forums, communities, stackoverflow, and google can't get out anyway. Secondly, this is the only official document, which is also a trick. The following is a code fragment that I copied from the official document, with little modification.
#-*-encoding: utf-8 -*-
# Encoding =utf-8
Import lean cloud
APP _ ID = ' temv 66 wvy 83 zurm 37 10 wt9 kir 4 1 zaza 5 gfu 0 gkhzzcf 4a 1yv '
APP _ KEY = ' 00 zuyjgtcvjs 3 1 QY 7 ysktd 43 fevoblt 4 yx 2 yjf 8gt 46 q 8d '
MASTER _ KEY = ' 82 irmrurzr 9 IB 2 axiputuh 6 ul 8 b 9 zozs 7 qbcfpchf 0 T2 ts 80 '
leancloud.init(APP_ID," %s/%s" % (APP_KEY,MASTER_KEY))
User = leancloud. User ()
User.set ("user name", "administrator")
user.set("password "," admin ")
user.set("email ","")
# Other fields can be set like using leancloud. target
User.set ("telephone", "4 15-392-0202")
Try:
user.sign_up()
With exceptions, e:
Print e
# Hooray! Now let them use the application.
Problem 1: SSL insertion platform warning
When running this program on Mac OS and Linux, the user registration fails, and the following warning will be observed at first:
/usr/lib/python 2.7/site-packages/requests-2 . 6 . 0-py 2.7 . egg/requests/packages/URL lib 3/util/SSL _。 Py: 79: inserteplatformwarning: The real SSLContext object is not available. This prevents urllib3 from configuring SSL correctly and may cause some SSL connections to fail. For more information, see.
Safety platform warning
However, after some searching, I found that this seems to be a common problem of urllib3, which can be solved in the following ways. But such a common problem, I can't even see a FAQ.
$ pip install pyopenssl ndg-https client pyasn 1
Question 2: sign_up () reports 40 1 error.
After solving the problem of 1, sign_up () always gives an error of 40 1 (unauthorized). After nearly an hour of positioning, it is found that this error is caused by the initialization function of leancloud. Usage in the programming guide is as follows:
leancloud.init(APP_ID," %s/%s" % (APP_KEY,MASTER_KEY))
But the reference manual says:
leancloud.client.init(APP_ID,APP_KEY,MASTER_KEY)
Neither method reports grammatical errors, but the former doesn't seem to work properly. It took a long time to find the API behind it, which is deceptive.
Question 3: sign_up () allows repeated registration.
The above test code, executed twice in a row, actually succeeded, and two identical user records can be seen in the background of leancloud. Sign_up () can be registered repeatedly by default? After searching for a long time, I found that the application options can configure options such as enabling mailbox verification for registered users. I checked and called sign_up for the second time to report an error.
Question 4: sign_up () cannot get the failure reason.
After catching the sign_up () exception, I tried to print it to get the reason for the failure, but the result was the following exception:
Backtracking (last call):
File "test 1.py", line 23, in< module & gt
Print e
UnicodeEncodeError: "ascii" codec cannot encode the character in position 6- 16: the sequence number is out of range (128).
Question 5: Python sdk has no relevant API to verify registered users' mailboxes or mobile phones.
After the user registers _, you can see that there will be user information in the _user table in the leancloud application background and a link to verify the mailbox, but what if my own python background provides this function? Pepsi can't understand.
Question 6: Grammatical errors in official documents.
The following is the routine of leancloud Python SDK programming guide. You can see that the fifth line should be game_score.save () instead of game.save ().
game_score = GameScore()
Game_score.set('score', 42) # or game_score.score = 42
game_score.set('cheatMode ',False)
game_score.set('playerName ',' Marvin ')
Games. Save ()
# You can also assign values when creating an object through keyword parameters.
game_score = GameScore(score=42,playerName='Marvin ')