Google翻译工具包API - google-api-java-client客户端登录403 Forbidden

时间:2011-01-23 02:42:29

标签: java google-data-api google-translator-toolkit

我刚刚使用google-api-java-client库实现了Google翻译工具包API。 问题是,我可以使用旧的“gdata”客户端库使用clientLogin进行身份验证,但我无法通过google-api-java-client进行身份验证。

这很简单,但我仍然得到403禁止回应。请求(旧/新)几乎相同,但只有auth令牌不同。谷歌只是发给我一个我无法通过身份验证的令牌......

请任何人帮忙,我花了一个小时完成整个模型,然后花了3个小时的时间。

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 个答案:

答案 0 :(得分:2)

所以,我在一小时内实现了翻译工具包api,然后我得到了 在clientLogin授权上停留了4个小时....

正确设置请求

gdataVersion = "1.0"; 
and GET request 

不幸的是,在尝试过程中,我有

1.0 and POST 

2.0 and GET 

这意味着gdataVersion = "2";仅适用于已经实施“新”客户端的API ... afaik