Google翻译工具包API-google-api-java-client客户端登录403禁止 [英] Google translator toolkit API - google-api-java-client Client login 403 Forbidden
问题描述
我刚刚使用google-api-java-client库实现了Google翻译工具包API. 问题是,我可以对旧的"gdata"客户端库使用clientLogin进行身份验证,但是我无法使用google-api-java-client进行身份验证.
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.
这很简单,但是我仍然收到403禁止响应.请求(旧/新)几乎相同,但只有身份验证令牌不同. Google只是向我发送了我无法通过其进行身份验证的令牌...
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...
请任何人帮助,我花了一个小时完成整个模型的实现,然后花了3个小时来解决这个问题.
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;
}
}
推荐答案
因此,我在一小时内实现了翻译工具包api, 在clientLogin授权上停留了4个小时....
So, I implemented translator toolkit api in an hour and then I got stuck for 4 hours on clientLogin authorization....
正确的请求设置是
gdataVersion = "1.0";
and GET request
不幸的是,在尝试过程中我遇到了
Unfortunately during the course of trying I had either
1.0 and POST
或
2.0 and GET
这意味着gdataVersion = "2";
仅适用于已实现新"客户端的API ... afaik
It means that gdataVersion = "2";
is working only for APIs for which the "new" client is already implemented...afaik
这篇关于Google翻译工具包API-google-api-java-client客户端登录403禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!