Skip to content

Instantly share code, notes, and snippets.

@ianhenrysmith
Created April 4, 2014 19:08
Show Gist options
  • Select an option

  • Save ianhenrysmith/9981195 to your computer and use it in GitHub Desktop.

Select an option

Save ianhenrysmith/9981195 to your computer and use it in GitHub Desktop.
Ivona Api Interaction
module Service
class IvonaCookie
require 'digest/md5'
require 'json'
require 'faraday'
EMAIL = '<email>'
REST_API_PATH = 'api/saas/rest'
API_KEY = '<api key>' # for ivona
def get_speech_file(message)
token = get_token
md5 = get_md5(token)
response = create_speech_file(message, token, md5)
JSON.parse(response.body)["soundUrl"]
end
def get_voices
token = get_token
md5 = get_md5(token)
response = connection.get do |req|
req.url "#{REST_API_PATH}/voices"
req.params['token'] = token
req.params['md5'] = md5
end
end
def get_token
response = connection.post do |req|
req.url "#{REST_API_PATH}/tokens"
req.params['email'] = EMAIL
end
response.body[1..response.body.length-2] # nasty sorry
end
def create_speech_file(message, token, md5)
connection.post do |req|
req.url "#{REST_API_PATH}/speechfiles"
req.params['text'] = message
req.params['token'] = token
req.params['md5'] = md5
req.params['contentType'] = 'text/plain'
req.params['voiceId'] = 'en_in_raveena'
req.params['codecId'] = 'mp3/22050'
end
end
def get_md5(token)
md5_api_key = Digest::MD5.hexdigest(API_KEY)
Digest::MD5.hexdigest(md5_api_key + token)
end
def connection
@conn ||= Faraday.new(url: 'http://api.ivona.com/') do |faraday|
faraday.request :url_encoded
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
end
end
end
@whoisstan
Copy link

I am using a valid API key and email. The token request gives me nicely a token. But then the call

http://api.ivona.com/api/saas/rest/speechfiles

fails with

{"code":"ERR_INVALID_TOKEN","description":"Invalid/inactive token. Generate new token using the getToken() method.","method":"listSpeechFiles"}

Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment