google translate api with oauth2.0

Viewed 7

I have a low-volume chat server. Some of the customers speak different languages so I have been using google translate and it had been working fine for years up to a couple weeks ago. I had been using a simple program like this:


// mcs -debug -out:TestTranslate.exe TestTranslate.cs
// mono --debug TestTranslate.exe

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

public class TestTranslate {

    public static void Main (string[] args)
    {
        string apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

        string message = "hello";
        string srclc = "en";
        string dstlc = "fr";

        string query = "key=" + UrlEncode (apikey) +
                       "&q="  + UrlEncode (message) +
                       "&source=" + UrlEncode (srclc) +
                       "&target=" + UrlEncode (dstlc);

        WebRequest request = WebRequest.Create ("https://www.googleapis.com/language/translate/v2?" + query);
        request.Method     = "GET";
        request.Timeout    = 3000;
        string reply       = new StreamReader (request.GetResponse ().GetResponseStream ()).ReadToEnd ();

        Console.WriteLine (reply);
    }

    public static string UrlEncode (string text)
    {
        return text.Replace (" ", "+");
    }

...but now it gets a '403' error. I suspect it is because I am not using OAuth2.0. I can't make any sense of the Google doc, it just goes round and round. Does anyone know how to make it work? I'm pretty sure I am using what they call 'service account', ie, one of my servers talking to one of their servers without any end-user authentication. I was able to download the OAuth2.0 json credential file. I would like to use C#/mono but Java is ok too.

0 Answers
Related