To call Azure Marketplace Translation (and Speech) service

Dependencies:   EthernetInterface-FRDM HTTPClient-SSL R_BSP SDFileSystem TLV320_RBSP USBHost mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AdmAuthentication.cpp Source File

AdmAuthentication.cpp

00001 ////////////////////////////////////////////////////////////////////////////
00002 //  Licensed under the Apache License, Version 2.0 (the "License");
00003 //  you may not use this file except in compliance with the License.
00004 //  You may obtain a copy of the License at
00005 //
00006 //    http://www.apache.org/licenses/LICENSE-2.0
00007 //
00008 //  Unless required by applicable law or agreed to in writing, software
00009 //  distributed under the License is distributed on an "AS IS" BASIS,
00010 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00011 //  See the License for the specific language governing permissions and
00012 //  limitations under the License.
00013 //
00014 //  Copyright (c) Microsoft Corporation.  All rights reserved.
00015 //  Portions Copyright (c) Kentaro Sekimoto All rights reserved.
00016 //
00017 //  Based on Text-To-Speech for .NET Micro Framework with Microsoft 
00018 //  Translator Service
00019 //  https://code.msdn.microsoft.com/Text-To-Speech-for-NET-9cea4462
00020 //
00021 ////////////////////////////////////////////////////////////////////////////
00022 
00023 #include "mbed.h"
00024 #include "AdmAuthentication.h"
00025 #include "HTTPClient.h"
00026 
00027 #define MAX_DATAIN  1024
00028 #define DATAMARKET_ACCESS_URI "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
00029 #define SCOPE   "http://api.microsofttranslator.com"
00030 #define HEADER  "application/x-www-form-urlencoded"
00031 #define TOKENTAG    "access_token"
00032 
00033 void urlencode(char *src, char *dst);
00034 
00035 static char datain[MAX_DATAIN];
00036 static HTTPText inText(datain, MAX_DATAIN);
00037 static char token_buf[MAX_DATAIN];
00038 
00039 char *AdmAuthentication::HttpPost(char *DatamarketAccessUri, char *requestDetails)
00040 {
00041     HTTPClient hc;
00042     HTTPText outText(requestDetails);
00043     outText.setDataType(HEADER);
00044 #ifdef DEBUG_AUTH
00045     printf("DatamarketAccessUri:%s\r\n", DatamarketAccessUri);
00046     printf("requestDetails:%s\r\n", requestDetails);
00047 #endif
00048     hc.post((const char*)DatamarketAccessUri, outText, &inText);
00049 #ifdef DEBUG_AUTH
00050     printf("inText:%s\r\n", datain);
00051 #endif
00052     char *pTokenStart = strstr(datain, TOKENTAG);
00053     if (pTokenStart == NULL) {
00054         return (char *)NULL;
00055     }
00056     pTokenStart += (strlen(TOKENTAG) + 3);
00057     char *pTokenEnd = strstr(pTokenStart, "\"");
00058     if (pTokenEnd == NULL) {
00059         return (char *)NULL;
00060     }
00061     strncpy(token_buf, pTokenStart, (pTokenEnd - pTokenStart));
00062     token_buf[pTokenEnd - pTokenStart] = 0;
00063 #ifdef DEBUG_AUTH
00064     printf("access_token:%s\r\n", token_buf);
00065 #endif
00066     return (char *)token_buf;
00067 }
00068 
00069 char *AdmAuthentication::GetAccessToken()
00070 {
00071     return HttpPost((char *)DATAMARKET_ACCESS_URI, request);
00072 }
00073 
00074 AdmAuthentication::AdmAuthentication(char *clientID, char *clientSecret)
00075 {
00076     char encoded_scope[64]; 
00077     id = clientID;
00078     secret = clientSecret;
00079     urlencode(id, encoded_id);
00080     urlencode(secret, encoded_secret);
00081     urlencode(SCOPE, encoded_scope);
00082     sprintf(request, "grant_type=client_credentials&client_id=%s&client_secret=%s&scope=%s", id, encoded_secret, SCOPE);
00083 }
00084 
00085 AdmAuthentication::~AdmAuthentication()
00086 {
00087 }