In the past two days, I wrote a Python script that automatically reads the word and adds the question to the DIY of OJ. One of the most painful parts is the image addition. No wall, and the previously used graph beds only have Flickr. For the first time, they are written using APIs.Code, Everything starts from scratch...
To use the Flickr API, you must first apply for an application from the QR code.Program, Flickr will provide an api_key and password api_secret
Afterwards, I downloaded Python-related third-party APIs and tried beejs' Python Flickr APIs and Flickr. PY, both APIs are quite depressing. All I need is to upload an image and obtain the image's external link. Neither of them can be directly implemented. Beej is officially specified by python. It does not directly obtain external links, and the code is too abstract and cannot be changed. the py code is much simpler, but there is no Image Upload method. So, do it on your own. the 1 K + line of Py has 200 lines of source code deleted, and the upload part of the code in beej has been moved. After a whole day, t_t has finally been completed.
The general principle of the official API is to get or post some values to the specified webpage, and then the server will return an XML data packet containing the information we need.
The first thing to do is to authenticate the identity and obtain permissions. I am working on a non-web application. Authentication requires three steps:
The first step is to get a frob value, which is clearly written in the official API. The method is "Flickr. Auth. getfrob", so we get the get URL:
Http://flickr.com/services/rest? Api_key = [api_key]& Method= Flickr. Auth. getfrob& Api_sig= [Api_sig]
[Api_key] is the api_key you applied for. For any get or POST request, there must be an api_sig value. This api_sig value is a verification of the previous parameter, the calculation method is your password api_secret +All parameters are sorted alphabetically by the connected strings.And then perform the value after md5hash. For example, the processed string is:
[Api_secret] api_key [api_key] methodflickr. Auth. getfrob
Obtain api_sig from the MD5 value.
A document in XML format is returned:
<?XML version = "1.0" encoding = "UTF-8"?><RSPStat= "OK"><Frob>72157629791093591-1120f17051e65424-60155050</Frob>
</RSP>
If stat is "OK", the request is valid. Otherwise, an error occurs in the request and an error message is returned, for example:
<?XML version = "1.0" encoding = "UTF-8"?><RSPStat= "Fail"><ErrCode= "97"MSG= "Missing signature" /></RSP>
After obtaining frob, perform Step 2 authentication:
Open the authentication page by calling the browser:
Http://flickr.com/servers/auth? Api_key = [api_key]& Perms= Write& Frob= [Frob]& Api_sig= [Api_sig] api_sig = MD5 ([api_secret] api_key [api_key] frob [frob] permswrite)
Perms = write indicates the upload permission. If it is read, it indicates the read-only permission. After the user logs on and authenticates, the user can perform step 3 authentication:
Step 3 is gettoken, method = 'flickr. Auth. gettoken'
Http://flickr.com/services/rest? Api_key = [api_key]& Frob= [Frob]& Method= [Method]& Api_sig= [Api_sig] api_sig = MD5 ([api_secret] api_key [api_key] frob [frob] method [Method])
Then return the result to get the token value. It is best to save the token value to a file. This token value is required for each operation in the future, and the authentication is successful here.
After the authentication is successful, you can perform various operations, see the official API method in detail: http://www.flickr.com/services/api/
The Image Upload method is different from other methods :'Photo' parameter should notIncluded in the signature. The signature must contain all other post parameters. Therefore, only the api_key and auth_token parameters are required.
It took a long time to upload the object. The error api_key is invalid when uploading the object using various methods. Therefore, we had to drag out the code of beej, the transmission of such large files must not be the same as that of small strings, but be sent in the form of multipart. I checked the multipart structure on the Internet:
The post information of multipart must be separated by "--" + boundary. boundary is a random string.
The data description is followed by the data. For cross-platform use, the linefeed is '\ r \ n '. Note that the last part is '--' + boundary + '--'.
In addition, you must add the sending type and boundary in the application header, as shown in the following section:
Content-Type: multipart/form-data; boundary = ttp6n0b1qzubjp6bk6gbnw
In this way, the message is successfully sent. The returned information contains the image ID. According to the ID, run the getinfo method (Flickr. Photos. getinfo) to obtain information similar to the following:
<? XML version = "1.0" encoding = "UTF-8" ?> < RSP Stat = "OK" > < Photo ID = "7008341001" Secret = "D20e29fbed" Server = "7191" Farm = "8" Dateuploaded = "1332508355" Isfavorite = "0" License = "0" Safety_level = "0" Rotation = "0" Views = "0" Media = "Photo" > < Owner Nsid = "60187189 @ n04" Username = "Amb. Flickr" Realname = "Amb w" Location = "" Iconserver = "6204" Iconfarm = "7" /> < Title > PIC </ Title > < Description /> < Visibility Ispublic = "1" Isfriend = "0" Isfamily = "0" /> < Dates Posted = "1332508355" Taken = "21:12:35" Takengranularity = "0" Lastupdate = "1332509039" /> < Permissions Permcomment = "3" Permaddmeta = "2" /> < Editability Cancomment = "1" Canaddmeta = "1" /> < Publiceditability Cancomment = "1" Canaddmeta = "0" /> < Usage Candownload = "1" Canblog = "1" Canprint = "1" Canshare = "0" /> < Comments > 0 </ Comments > < Notes /> < People Haspeople = "0" /> < Tags /> < URLs > < URL Type = "Photopage" > Http://www.flickr.com/photos/ambition0109/7008341001/ </ URL > </ URLs > </ Photo > </ RSP >
Get the secret, server, farm value contained in it to construct an external chain, the Construction Method to see here http://www.flickr.com/services/api/misc.urls.html
The above big picture chain is http://farm8.staticflickr.com/7191/7008341001_d20e29fbed_ B .jpg
In this way, the part of the QR code is completed ~~~
original address: http://www.cnblogs.com/ambition/archive/2012/04/11/2442802.html