To call Azure Marketplace Translation (and Speech) service
Dependencies: EthernetInterface-FRDM HTTPClient-SSL R_BSP SDFileSystem TLV320_RBSP USBHost mbed-rtos mbed-src
The program calls Azure Marketplace Translation (and Speech) service.
How to use - Create an account for Microsoft Azure Marketplace - Get client ID and client secret. - Change client ID and client secret in main.cpp
The program was created based on the following libraries.
CodePlex Text-To-Speech with Microsoft Translator Service http://translatorservice.codeplex.com/
MBED HTTPClient-SSL Library https://developer.mbed.org/teams/MultiTech/code/HTTPClient-SSL/
AdmAuthentication.cpp@1:a2bd45c3b373, 2015-11-07 (annotated)
- Committer:
- ksekimoto
- Date:
- Sat Nov 07 12:29:06 2015 +0000
- Revision:
- 1:a2bd45c3b373
- Parent:
- 0:40a09c55e5be
To call Azure Marketplace Translation (and Speech) service.; The first version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ksekimoto | 0:40a09c55e5be | 1 | //////////////////////////////////////////////////////////////////////////// |
ksekimoto | 0:40a09c55e5be | 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
ksekimoto | 0:40a09c55e5be | 3 | // you may not use this file except in compliance with the License. |
ksekimoto | 0:40a09c55e5be | 4 | // You may obtain a copy of the License at |
ksekimoto | 0:40a09c55e5be | 5 | // |
ksekimoto | 0:40a09c55e5be | 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
ksekimoto | 0:40a09c55e5be | 7 | // |
ksekimoto | 0:40a09c55e5be | 8 | // Unless required by applicable law or agreed to in writing, software |
ksekimoto | 0:40a09c55e5be | 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
ksekimoto | 0:40a09c55e5be | 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ksekimoto | 0:40a09c55e5be | 11 | // See the License for the specific language governing permissions and |
ksekimoto | 0:40a09c55e5be | 12 | // limitations under the License. |
ksekimoto | 0:40a09c55e5be | 13 | // |
ksekimoto | 0:40a09c55e5be | 14 | // Copyright (c) Microsoft Corporation. All rights reserved. |
ksekimoto | 0:40a09c55e5be | 15 | // Portions Copyright (c) Kentaro Sekimoto All rights reserved. |
ksekimoto | 0:40a09c55e5be | 16 | // |
ksekimoto | 0:40a09c55e5be | 17 | // Based on Text-To-Speech for .NET Micro Framework with Microsoft |
ksekimoto | 0:40a09c55e5be | 18 | // Translator Service |
ksekimoto | 0:40a09c55e5be | 19 | // https://code.msdn.microsoft.com/Text-To-Speech-for-NET-9cea4462 |
ksekimoto | 0:40a09c55e5be | 20 | // |
ksekimoto | 0:40a09c55e5be | 21 | //////////////////////////////////////////////////////////////////////////// |
ksekimoto | 0:40a09c55e5be | 22 | |
ksekimoto | 0:40a09c55e5be | 23 | #include "mbed.h" |
ksekimoto | 0:40a09c55e5be | 24 | #include "AdmAuthentication.h" |
ksekimoto | 0:40a09c55e5be | 25 | #include "HTTPClient.h" |
ksekimoto | 0:40a09c55e5be | 26 | |
ksekimoto | 0:40a09c55e5be | 27 | #define MAX_DATAIN 1024 |
ksekimoto | 0:40a09c55e5be | 28 | #define DATAMARKET_ACCESS_URI "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13" |
ksekimoto | 0:40a09c55e5be | 29 | #define SCOPE "http://api.microsofttranslator.com" |
ksekimoto | 0:40a09c55e5be | 30 | #define HEADER "application/x-www-form-urlencoded" |
ksekimoto | 0:40a09c55e5be | 31 | #define TOKENTAG "access_token" |
ksekimoto | 0:40a09c55e5be | 32 | |
ksekimoto | 0:40a09c55e5be | 33 | void urlencode(char *src, char *dst); |
ksekimoto | 0:40a09c55e5be | 34 | |
ksekimoto | 0:40a09c55e5be | 35 | static char datain[MAX_DATAIN]; |
ksekimoto | 0:40a09c55e5be | 36 | static HTTPText inText(datain, MAX_DATAIN); |
ksekimoto | 0:40a09c55e5be | 37 | static char token_buf[MAX_DATAIN]; |
ksekimoto | 0:40a09c55e5be | 38 | |
ksekimoto | 0:40a09c55e5be | 39 | char *AdmAuthentication::HttpPost(char *DatamarketAccessUri, char *requestDetails) |
ksekimoto | 0:40a09c55e5be | 40 | { |
ksekimoto | 0:40a09c55e5be | 41 | HTTPClient hc; |
ksekimoto | 0:40a09c55e5be | 42 | HTTPText outText(requestDetails); |
ksekimoto | 0:40a09c55e5be | 43 | outText.setDataType(HEADER); |
ksekimoto | 0:40a09c55e5be | 44 | #ifdef DEBUG_AUTH |
ksekimoto | 0:40a09c55e5be | 45 | printf("DatamarketAccessUri:%s\r\n", DatamarketAccessUri); |
ksekimoto | 0:40a09c55e5be | 46 | printf("requestDetails:%s\r\n", requestDetails); |
ksekimoto | 0:40a09c55e5be | 47 | #endif |
ksekimoto | 0:40a09c55e5be | 48 | hc.post((const char*)DatamarketAccessUri, outText, &inText); |
ksekimoto | 0:40a09c55e5be | 49 | #ifdef DEBUG_AUTH |
ksekimoto | 0:40a09c55e5be | 50 | printf("inText:%s\r\n", datain); |
ksekimoto | 0:40a09c55e5be | 51 | #endif |
ksekimoto | 0:40a09c55e5be | 52 | char *pTokenStart = strstr(datain, TOKENTAG); |
ksekimoto | 0:40a09c55e5be | 53 | if (pTokenStart == NULL) { |
ksekimoto | 0:40a09c55e5be | 54 | return (char *)NULL; |
ksekimoto | 0:40a09c55e5be | 55 | } |
ksekimoto | 0:40a09c55e5be | 56 | pTokenStart += (strlen(TOKENTAG) + 3); |
ksekimoto | 0:40a09c55e5be | 57 | char *pTokenEnd = strstr(pTokenStart, "\""); |
ksekimoto | 0:40a09c55e5be | 58 | if (pTokenEnd == NULL) { |
ksekimoto | 0:40a09c55e5be | 59 | return (char *)NULL; |
ksekimoto | 0:40a09c55e5be | 60 | } |
ksekimoto | 0:40a09c55e5be | 61 | strncpy(token_buf, pTokenStart, (pTokenEnd - pTokenStart)); |
ksekimoto | 0:40a09c55e5be | 62 | token_buf[pTokenEnd - pTokenStart] = 0; |
ksekimoto | 0:40a09c55e5be | 63 | #ifdef DEBUG_AUTH |
ksekimoto | 0:40a09c55e5be | 64 | printf("access_token:%s\r\n", token_buf); |
ksekimoto | 0:40a09c55e5be | 65 | #endif |
ksekimoto | 0:40a09c55e5be | 66 | return (char *)token_buf; |
ksekimoto | 0:40a09c55e5be | 67 | } |
ksekimoto | 0:40a09c55e5be | 68 | |
ksekimoto | 0:40a09c55e5be | 69 | char *AdmAuthentication::GetAccessToken() |
ksekimoto | 0:40a09c55e5be | 70 | { |
ksekimoto | 0:40a09c55e5be | 71 | return HttpPost((char *)DATAMARKET_ACCESS_URI, request); |
ksekimoto | 0:40a09c55e5be | 72 | } |
ksekimoto | 0:40a09c55e5be | 73 | |
ksekimoto | 0:40a09c55e5be | 74 | AdmAuthentication::AdmAuthentication(char *clientID, char *clientSecret) |
ksekimoto | 0:40a09c55e5be | 75 | { |
ksekimoto | 0:40a09c55e5be | 76 | char encoded_scope[64]; |
ksekimoto | 0:40a09c55e5be | 77 | id = clientID; |
ksekimoto | 0:40a09c55e5be | 78 | secret = clientSecret; |
ksekimoto | 0:40a09c55e5be | 79 | urlencode(id, encoded_id); |
ksekimoto | 0:40a09c55e5be | 80 | urlencode(secret, encoded_secret); |
ksekimoto | 0:40a09c55e5be | 81 | urlencode(SCOPE, encoded_scope); |
ksekimoto | 0:40a09c55e5be | 82 | sprintf(request, "grant_type=client_credentials&client_id=%s&client_secret=%s&scope=%s", id, encoded_secret, SCOPE); |
ksekimoto | 0:40a09c55e5be | 83 | } |
ksekimoto | 0:40a09c55e5be | 84 | |
ksekimoto | 0:40a09c55e5be | 85 | AdmAuthentication::~AdmAuthentication() |
ksekimoto | 0:40a09c55e5be | 86 | { |
ksekimoto | 0:40a09c55e5be | 87 | } |