2011-01-23 42 views
0

我剛剛使用google-api-java-client庫實現了Google翻譯工具包API。 問題是,我可以使用clientLogin與舊的「gdata」客戶端庫進行身份驗證,但我無法通過google-api-java-client進行設置。Google翻譯工具包API - google-api-java-client客戶端登錄403 Forbidden

這很簡單,但我仍然得到403禁止的迴應。請求(舊/新)幾乎相同,但只有認證令牌不同。谷歌只是給我一個令牌,我無法驗證...

請任何人的幫助,我花了整整一個小時的模型實現,然後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 = "[email protected]"; 
    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; 
} 
} 

回答

2

所以,我實現了譯者工具包API的一個小時,然後我得到了 卡上的ClientLogin認證4小時....

的正確設置要求是

gdataVersion = "1.0"; 
and GET request 

不幸的是,在嘗試的過程中,我有

1.0 and POST 

2.0 and GET 

這意味着gdataVersion = "2";是針對其「新」的客戶端已經實現API的唯一的工作......據我所知