Rest combat--call the cloud service of Baidu Voice

Source: Internet
Author: User
Tags 0xc0 oauth

Restful

REST (Representation State Transfer) describes a schema-style network system, such as a Web application. It first appeared in 2000 with Roy Thomas Fielding's doctoral dissertation, one of the main authors of the HTTP specification. Rest refers to a set of schema constraints and principles. The application or design that satisfies these constraints and principles, that is, restful is the rest style. In rest, a resource is the core, and anything, as long as it is necessary to be referenced, is a resource. Each resource must have at least one Uniform Resource identifier, that is, Uri,uri is both the name of the resource and the address of the resource. The relationship between the URI and the resource is many-to-one, meaning that a URI identifies only one resource, but a resource can have an extra URI. The resources in rest are a combination of data and representations, and the idea of resource-centric design is at the heart of rest. A resource is an information entity that can have a variety of external representations. We take the form of "resources" specifically, called its presentation layer (representation). If the client wants to operate the server, there must be a way to make the server side "state Transfer". And this transformation is based on the expression layer, so is the "Expression Layer state transformation." The method that the client uses, can only be the HTTP protocol. Specifically, there are four verbs in the HTTP protocol that represent the mode of operation: GET, POST, PUT, DELETE. They correspond to four basic operations: get is used to get a resource, post is used to create a new resource (and can also be used to update resources), put is used to update resources, and delete is used to delete resources. The rest specification includes: client-server, stateless, cache, unified interface, tiered system, and on-demand code. These architectural constraints are literal translation from Fielding's doctoral dissertation, so it is highly recommended to read Roy Thomas Fielding's doctoral dissertation to have a deeper understanding of rest.

Currently, restful Web services have been used in a number of areas, and some large companies have provided rest-based network service. For example, the foreign Google, Amazon, etc., the domestic Baidu and other companies. The following is a more interesting speech recognition cloud service as an example, the service is provided by Baidu http://yuyin.baidu.com/ .

Baidu Voice Cloud Service

Baidu's voice recognition service provides developers with a common HTTP interface through RESTAPI. One of the benefits of doing this is lightweight . The lightweight meaning here is that there is no need to integrate any SDK into the developed application or to add any recognition engines to the test machine. Developers only need to understand the HTTP network request and the use of the Baidu Voice Rest API rules, you can implement speech recognition in their own applications.

Of course, before using the Voice cloud service Baidu, must be to do the developer registration application, after the application Baidu will provide developers with an API key and secret key, in the late with the Baidu server communication will use two Key. Before using Baidu's speech recognition REST API, you need to get an access Token. This access Token is a credential for user authentication and authorization. Baidu's speech recognition uses the client Credentials (http://developer.baidu.com/wiki/index.php?title=docs/oauth/client) Authorization method, That is, the use of the public key, the key to obtain access token, for any application with the server type, access token obtained through this authorization can only access the Platform authorization class interface, that is, through it gets access Tokens can only be used to access the user-independent open APIs. The way to get access tokens is also simple. In accordance with the requirements of Baidu, Baidu's server to send a specific format of request packets, Baidu's server will return a response packet, the response packet contains JSON format data, one of the fields is Access_token.

Get access token need to apply to Baidu post a OAuth2.0 authorization service request, post to the address of Https://openapi.baidu.com/oauth/2.0/token, also need to bring the following parameters: Grant_ Type: Must be a parameter, fixed to "client_credentials"; client_id: Required parameter, which is the API Key;client_secret provided by Baidu to developers when they apply for it: Must parameters, That is, when developers apply for Baidu to provide developers with secret Key;scope: non-required parameters, is a space-delimited list of permissions. For example, a standard POST request packet has the following format:

Https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq4eR3LT0vuXV4 &client_secret= 0RDSJZQ20XUJ5ITV7WRTZNPQSZR5PVW2

In the event that the request parameter is correct, the Baidu server returns a JSON-formatted text with the following fields: Access_token: The validity period of the access Token;expires_in:access token to get, in seconds; refresh_ Token: Used to refresh access token, all apps will return this parameter, Scope:access token's final access scope, which is the list of permissions actually granted by the user (the user may be able to cancel some of the requested permissions when authorizing the page). For specific information about permissions refer to the "Permission List" section; Session_key: The session key required to invoke the Open API based on HTTP, whose expiration date is consistent with access token; Session_secret: Invoke open based on HTTP The signature key used to calculate the parameter signature for the API. For example, a response packet returned by the Baidu server is as follows:

http/1.1 Okcontent-type:application/jsoncache-control:no-store {"Access_token": "1. a6b7dbd428f731035f771b8d15063f61.86400.1292922000-2346678-124328 ",    " expires_in ": 86400," Refresh_token ":" 2.385d55f8615fdfd9edb7c4b5ebdc3e39.604800.1293440400-2346678-124328 ",    " scope ":" Public ",    " Session_key ": "Anxxsnjwqdugf8615onqeikrmu2bkaxcdllxn",    "Session_secret": "248APXVXJCZ0VEC43EYRVXQAK4OZEXMB",}

If the request is wrong, Baidu's server will also return a JSON-formatted text containing the following parameters: Error code, which is the wrong type of code; Error_description: Error description information to help understand and resolve the error that occurred. For example, the following Request response packet:

http/1.1 Requestcontent-type:application/jsoncache-control:no-store {    "error": "Invalid_grant", "Error_ Description ":" Invalid Authorization Code:anxxsnjwqdugonqeikrmu2bkaxcdllxn "}

When you want to use C or C + + to do the work, there is a powerful tool: curl. Curl is an open source file Transfer tool that works with URL syntax in the command line mode. It is widely used in Unix, multiple Linux distributions, and has a ported version of DOS and Win32, Win64. There is also a corresponding development library function,--libcurl, which provides developers with a wealth of library functions to communicate with the server. For example, to use curl to get Baidu's access Token, the method is very simple, using the Curl–s command to Baidu Server post a request can be achieved, I am on my own machine experiments such as:

By


, you can see that the result of the response is a JSON-formatted packet. Where the "Access_token" field is the token required to request the rest API, access token is valid for one months by default, and developers need to judge the validity of Access tokens if access token expires.
want to use Baidu's voice recognition service to identify a local voice is also very simple, directly to the Baidu server post their own voice data, the other side of the server will return a piece of JSON-formatted text, the corresponding field is the result of recognition, the results can be resolved to use the results of recognition. The
Baidu Voice cloud service supports Post's way to upload voice data; the rest API of Baidu Voice only supports the whole speech recognition mode, that is, it needs to upload whole speech for recognition. There are several compression formats for voice data: PCM (uncompressed), WAV, opus, Speex, Amr, X-flac. There are two ways to upload voice data: Implicit sending and display sending. The so-called implicit transmission is to format the voice data into standard JSON format data, through post upload, here the JSON format specifies the corresponding fields, of course, before the format of the voice data needs to be BASE64 encoded. The so-called display sends the voice data directly in the Http-body, control rest parameters and related statistics can be passed through the rest API. For the above two ways of uploading, Baidu's server will return a unified result, are packaged in JSON format. If the recognition succeeds, the recognition result is placed in the "result" field of the JSON, and is encoded uniformly in utf-8 mode.
for all HTTP actions above, there are corresponding library functions in the Liburl library, so it is very convenient to use. An example is given below. The use of Baidu's voice recognition service to post a local PCM format of voice data to Baidu server, the content of voice data is "Baidu voice to provide technical support." Source code is also very simple, mainly is Base64 encoding, JSON format data parsing and the use of Libcurl library functions for post, get operations. Environment is under Ubuntu, of course, before using the Jsoncpp library and the Libcurl library, installation method is very simple, there are a lot of online. This is directly affixed to the source code:

#include <cstdio> #include <cstring> #include <stdlib.h> #include "curl/curl.h" #include "curl/easy.h "#include" json/json.h "#define Max_buffer_size 512#define max_body_size 1000000using namespace std;static const std:: String base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static inline bool Is_ Base64 (unsigned char c) {return (Isalnum (c) | | (c = = ' + ') | | (c = = '/'));} String Base64_encode (unsigned char const* bytes_to_encode, unsigned int in_len) {std::string Ret;int i = 0;int j = 0;unsig Ned Char char_array_3[3];unsigned Char char_array_4[4];while (in_len--) {char_array_3[i++] = * (bytes_to_encode++); if (i = = 3) {Char_array_4[0] = (char_array_3[0] & 0XFC) >> 2;char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((Char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((Char_array_ 3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f;for (i = 0; (I <4); i++) ret + = Base64_chars[char_array_4[i]];i = 0;}} if (i) {for (j = i; J < 3; j + +) char_array_3[j] = ' + '; char_array_4[0] = (char_array_3[0] & 0XFC) >> 2;char_arr AY_4[1] = ((char_array_3[0] & 0x03) << 4) + ((Char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_a RRAY_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f ; for (j = 0; (J < i + 1); J + +) ret + = Base64_chars[char_array_4[j]];while ((i++ < 3)) ret + = ' = ';} return ret;} String Base64_decode (std::string const& encoded_string) {int in_len = encoded_string.size (); int i = 0;int j = 0;int in _ = 0;unsigned Char char_array_4[4], char_array_3[3];std::string ret;while (in_len--&& (encoded_string[in_]! = ' = ') && is_base64 (Encoded_string[in_]) {char_array_4[i++] = Encoded_string[in_]; in_++;if (i = = 4) {for (i = 0; i <4; i++) Char_array_4[i] = Base64_chars.find (Char_array_4[i]); char_array_3[0] = (Char_array_4[0] << 2) + ((Char_array_4[1] & 0x30) >> 4) char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((Char_ar RAY_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];for (i = 0; (I < 3); i++) ret + = Char_array_3[i];i = 0;}} if (i) {for (j = i; J <4; J + +) Char_array_4[j] = 0;for (j = 0; J <4; J + +) Char_array_4[j] = Base64_chars.find (Char_arra Y_4[J]) char_array_3[0] = (Char_array_4[0] << 2) + ((Char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((cha R_ARRAY_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2) char_array_3[2] = ((Char_array_4[2] & 0x3) << 6) + char_array_4[3];for (j = 0; (J < i-1); J + +) ret + = Char_array_3[j];} return ret;} callback function static size_t writefunc (void *ptr, size_t size, size_t nmemb, char **result) {size_t Result_len = size * Nmemb;*resul t = (char *) realloc (*result, Result_len + 1), if (*result = = NULL) {printf ("realloc failure!\n"); return 1;} memcpy (*resuLT, ptr, Result_len);(*result) [Result_len] = ' <<*result<<endl;/*json ';cout<< the Json data returned by the Baidu server:: Reader reader; Json::value root;if (Reader.parse (Result,root)) {String res = root["Result"].asstring (); cout << "parsed result:" << Res << Endl;} */return Result_len;} int main () {freopen ("OUT.txt", "w", stdout); int json_file_size; FILE *pfile = Null;char *audio_data;pfile = fopen ("TEST.PCM", "R"), if (pFile = = NULL) {perror ("Open file error!\n");} Else{fseek (pFile, 0, seek_end); int file_size = Ftell (pFile); cout << "File size:" << file_size << "byte S "<< Endl;fseek (pFile, 0, seek_set); audio_data = (char *) malloc (sizeof (char) *file_size); Fread (Audio_data, File_ Size, sizeof (char), pFile);//MAC address of machine char *cuid = "56:84:7a:fe:97:99"; char *api_key = "6yfhyifmjxc8qmubiicxbqgi"; char * Secret_key = "nzn45o3x0lgx42qovumyy2mjpoioup2e"; Char host[max_buffer_size];snprintf (host, sizeof (host), "https:// Openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s ", Api_key, Secret_key); cout <<" host of curl-s Command: "<< host << Endl; FILE *p = Null;char cmd[max_buffer_size];//curl-s command Returns the result of char *result = (char*) malloc (max_buffer_size); char *curl_cmd = " Curl-s "; char *yinhao =" \ ""; strcpy (cmd, curl_cmd); strcat (cmd, Yinhao); strcat (cmd, host); strcat (cmd, yinhao);p = Popen (c MD, "R"); Fgets (result, max_buffer_size, p); cout << "curl-s response Result:" << result << endl;pclose (p); string A ccess_token;//the Json data returned by the parsing server, gets the access_tokenif (result! = NULL) {Json::reader Reader; Json::value root;if (Reader.parse (result, root, False)) {Access_token = Root.get ("Access_token", ""). Asstring (); cout << "Access_token:" << access_token << Endl;} Take the implicit send to the server to send JSON format data char body[max_body_size];memset (body, 0, sizeof (body)); string decode_data = Base64_encode ( (const unsigned char *) audio_data, file_size); if (0 = = Decode_data.length ()) {cout << "error!base64 encoded data is E Mpty! "; return 1;}else{json::value buffer; Json::fastwriter buf_writer;buffer["format"] = "PCM"; buffer["rate"] = 8000;buffer["channel"] = 1;buffer["token"] = Access_token.c_str (); buffer["cuid"] = cuid;buffer["speech"] = decode_data;buffer["len"] = file_size;// The length of the actual Json format Data json_file_size = buf_writer.write (buffer). Length (); cout << "Json file Size:" << json_file_ Size << "bytes" << endl;memcpy (body, buf_writer.write (buffer). C_STR (), json_file_size); CURL *curl; Curlcode res;//Server response result char *result_buffer = null;struct curl_slist *http_header = Null;char Temp[max_buffer_size]; memset (temp, 0, sizeof (temp)), snprintf (temp, sizeof (temp), "%s", "Content-type:application/json; Charset=utf-8 "); Http_header = Curl_slist_append (Http_header, temp); snprintf (temp, sizeof (temp)," Content-length:%d " , json_file_size); http_header = Curl_slist_append (Http_header, temp), memset (host, 0, sizeof), snprintf (host, sizeof (host), "%s", "Http://vop.baidu.com/server_api"); cout << "Server Host:" << host << Endl;curl = Curl_easy_init (); curl_easy_setopt (curl, Curlopt_url, host);//Set Access urlcurl_easy_setopt ( Curl, Curlopt_post, 1);//1 represents a regular HTTP POST request curl_easy_setopt (Curl, curlopt_timeout, 30);//Set delay curl_easy_setopt (curl, Curlopt_httpheader, Http_header); curl_easy_setopt (curl, curlopt_postfields, body); Curl_easy_setopt (Curl, CURLOPT_ Postfieldsize, json_file_size); curl_easy_setopt (Curl, curlopt_writefunction, writefunc);//Set callback function Curl_easy_setopt ( Curl, Curlopt_writedata, &result_buffer), res = Curl_easy_perform (curl), if (res! = CURLE_OK) {printf ("Perform curl Error:%d.\n ", res); return 1;} Curl_slist_free_all (Http_header); Curl_easy_cleanup (curl); free (audio_data);}} Fclose (pFile); return 0;}

Because the additional library functions are used, the link parameters are added at compile time, and the compile command is as follows:

G++-o Bdvoice baiduvr.cpp-l. -L Json_linux-gcc-4.8_libmt-l Curl

Note: Here will jsoncpp the header file of the library under the JSON folder and the header file of the Libcurl library under the Curl folder, and the corresponding. A and. So libraries are placed in a directory with the code files. Run the executable file and write the standard output to the Out.txt,out.txt in the source code as follows:





Rest combat--call the cloud service of Baidu Voice

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.