accessing google translate api

  1. 2 years ago

    dave d

    2 May 2017 Pre-Release Testers, Xojo Pro Australia

    i see google have an API for translate
    https://cloud.google.com/translate/docs/reference/translate

    However, i'm not even sure where to start figuring out how to get Xojo to access it, I've looked at XOJO web services and have got no further, has anyone managed to access the API.

  2. Edited 2 years ago

    You'll need httpsocket and json.

    Also, review this page...

    https://cloud.google.com/docs/authentication

  3. John J

    2 May 2017 Pre-Release Testers, Xojo Pro Cleveland, Ohio

    I don't know if this is helpful at all, but I will post it here in the hopes that it is. This is a code snippet using a socket and JSON to connect to an API. This is a totally different API - but it may give you a bit of an idea how things work. It is generally pretty simple once you get the core concepts that Greg mentioned above: HTTPsocket and JSONitem .

    Hope it helps,
    J

    Dim s as new HTTPSecureSocket
    dim responseJSON as new JSONItem
    
    try
      dim getProductInfo as new JSONItem
      s.SetRequestHeader("Authorization", me.authHeaderString)
      
      // Get current product info from API
      dim CurrentProductResponse as string = s.Get(me.BaseURL + "products/" + id.ToText, 40)
      getProductInfo.Load(CurrentProductResponse)
      dim currentVariations as new JSONItem
      currentVariations = getProductInfo.Value("variations")
      
      // Create a dictionary of website IDs to SKUs
      dim skuToID as new Dictionary
      skuToID.Value(getProductInfo.Value("sku")) = getProductInfo.Value("id")
      
      for i as integer = 0 to currentVariations.Count - 1
        skuToID.Value(currentVariations.child(i).Value("sku")) = currentVariations.child(i).Value("id")
        
      next
      
      // Take out any existing SKUs and replace with IDs
      // Base product must exist or we wouldn't be here
      myJSON.Remove("sku")
      myJSON.Value("id") = id
      
      dim updatedVariations as new JSONItem
      updatedVariations = myJSON.Value("variations")
      
      for i as integer = 0 to updatedVariations.Count -1
        if skuToID.HasKey(updatedVariations.child(i).Value("sku")) then
          updatedVariations.child(i).Value("id") = skuToID.Value(updatedVariations.child(i).Value("sku"))
          updatedVariations.child(i).Remove("sku")
        end
      next
      
      // Post it baby!
      s.SetRequestContent(myJSON.ToString, "application/json; charset=UTF-8")
      Dim response As String = s.Post(me.BaseURL + "products/" + id.ToText, 40)
      responseJSON.Load(response)
      
    catch
      Errors.Append("Bad response from API: Update Item")
      
    end try
    
    responseJSON.Value("ResponseCode") = s.HTTPStatusCode
    
    return responseJSON
    
  4. 8 months ago

    James M

    15 Apr 2018 Pre-Release Testers, Xojo Pro

    @dave d i see google have an API for translate
    https://cloud.google.com/translate/docs/reference/translate

    However, i'm not even sure where to start figuring out how to get Xojo to access it, I've looked at XOJO web services and have got no further, has anyone managed to access the API.

    Here is some code that seems to work.... It uses MonkeyBread CURL but it should be easy to convert to Xojo built-in socket... You will also need to get your own API key see: https://support.google.com/cloud/answer/6158857?hl=en

    dim apikey as string = "put your API key here"
    
    Dim payloadJSON As New JSONItem
    payloadJSON.Value("source") = "en"
    payloadJSON.Value("target") = "es"
    payloadJSON.Value("format") = "text"
    payloadJSON.Value("q") = "This is a test of the emergency broadcast system.  this is only a test."
    payloadJSON.Value("key") = apikey
    
    dim payload as string = payloadJSON.ToString
    
    dim c as new CURLSMBS
    
    c.CollectDebugData = true
    c.CollectHeaderData = true
    c.CollectOutputData = true
    c.OptionVerbose = true
    c.OptionSSLVersion = CURLSMBS.kSSLVersionTLSv1
    
    
    c.OptionURL = "https://www.googleapis.com/language/translate/v2?key="+apikey
    
    c.OptionConnectionTimeout = 20
    c.OptionSSLVerifyHost = 0
    c.OptionSSLVerifyPeer = 0
    c.OptionPostFields = payload
    
    dim Headers() as string
    dim header(-1) as string
    header.Append "Content-Type: application/json"
    c.SetOptionHTTPHeader header
    
    
    dim error as integer = c.Perform
    dim output as string = c.OutputData
    dim debug  as string = c.DebugData
    
    beep
  5. Michel B

    15 Apr 2018 Pre-Release Testers RubberViews.com
    Edited 8 months ago

    Someone had managed to access that API a while ago, and tried to peddle his own product in this forum as working with his own translation engine. Sure, Google probably worked decades on it's own engine, and here come this guy who did the same in his kitchen. Yeah, right :D.

    He was quickly exposed by forum members.

    But indeed, that was a good proof of concept for the use of the Google API.

or Sign Up to reply!