Google translator toolkit API - google-api-java-client Client login 403 Forbidden

浪子不回头ぞ 提交于 2019-12-25 16:08:46

问题


I just implemented Google translator toolkit API using google-api-java-client library. The problem is, that I can authenticate using clientLogin with the old "gdata" client library, but I can't manage to do that with google-api-java-client.

It's quite straightforward, but I'm still getting 403 forbidden response. The requests (old / new) are almost the same, but only the auth tokens differ. Google just sends me a token that I cannot authenticate with...

Please anybody help, I spent an hour with the entire model implementation and then 3 hours of this hell.

public class GttClient {
 public static void main(String[] args) {

  Debug.enableLogging();
  HttpTransport transport = setUpTransport();

  try {
   authenticateWithClientLogin(transport);
   printResults(executeGet(transport, GttUrl.forDocuments()));
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private static HttpTransport setUpTransport() {
  HttpTransport transport = GoogleTransport.create();
  GoogleHeaders headers = (GoogleHeaders) transport.defaultHeaders;
  headers.setApplicationName("Google-PredictionSample/1.0");
  headers.gdataVersion = "2.0";
  AtomParser parser = new AtomParser();
  parser.namespaceDictionary = Namespace.DICTIONARY;
  transport.addParser(parser);
  return transport;
 }

 private static void authenticateWithClientLogin(HttpTransport transport)
   throws IOException {
  ClientLogin clientLogin = new ClientLogin();
  clientLogin.authTokenType = "gtrans";
  clientLogin.accountType = "HOSTED_OR_GOOGLE";
  clientLogin.username = "user@gmail.com";
  clientLogin.password = "password";
  clientLogin.authenticate().setAuthorizationHeader(transport);
 }

 public static Feed executeGet(HttpTransport transport, GttUrl url)
   throws IOException {

  HttpRequest request = transport.buildGetRequest();
 // url.fields = GData.getFieldsFor(Feed.class);
  request.url = url;

  return request.execute().parseAs(Feed.class);
 }

}

public class GttUrl extends GoogleUrl {

 static final String ROOT_URL = "https://translate.google.com/toolkit/feeds";

 @Key("sharedwith")
 public String sharedwith;

 @Key("onlydeleted")
 public String onlydeleted;

 @Key("scope")
 public String scope;

 public GttUrl(String url) {
  super(url);
  if (Debug.ENABLED) {
   this.prettyprint = true;
  }
 }

 public static GttUrl forRoot() {
  return new GttUrl(ROOT_URL);
 }

 public static GttUrl forDocuments() {
  GttUrl result = forRoot();
  result.pathParts.add("documents");
  return result;
 }

 public static GttUrl forTranslMemories() {
  GttUrl result = forRoot();
  result.pathParts.add("tm");
  return result;
 }

 public static GttUrl forGlossaries() {
  GttUrl result = forRoot();
  result.pathParts.add("glossary");
  return result;
 }
}

回答1:


So, I implemented translator toolkit api in an hour and then I got stuck for 4 hours on clientLogin authorization....

the proper setup of the request is

gdataVersion = "1.0"; 
and GET request 

Unfortunately during the course of trying I had either

1.0 and POST 

or

2.0 and GET 

It means that gdataVersion = "2"; is working only for APIs for which the "new" client is already implemented...afaik



来源:https://stackoverflow.com/questions/4772008/google-translator-toolkit-api-google-api-java-client-client-login-403-forbidde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!