2

我尝试通过http://developer.yahoo.com/mail/code/上的示例为 JAX-WS 授权 yahoo api。我得到一个例外:

Exception in thread "main" ua.com.stormlabs.gap.yahoomail.BrowserBasedAuthManager$AuthException: Signature mismatch
at ua.com.stormlabs.gap.yahoomail.BrowserBasedAuthManager.authenticate(BrowserBasedAuthManager.java:59)
at ua.com.stormlabs.gap.yahoomail.JaxWsSample.main(JaxWsSample.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我的代码片段:

String token = "<very_big_token>";
BrowserBasedAuthManager authManager = new BrowserBasedAuthManager(appid, secret);
authManager.setToken(token);
authManager.authenticate();

和 authenticate() 方法:

try {
        long time = System.currentTimeMillis() / 1000;
        String signatureUrl = "/WSLogin/V1/wspwtoken_login?appid=" +
                URLEncoder.encode(this.appid, "UTF-8") + "&token=" + URLEncoder.encode(this.token, "UTF-8") +
                "&ts=" + time;
        MessageDigest digest = MessageDigest.getInstance("MD5");
        String signature = new BigInteger(1, digest.digest((signatureUrl + this.secret).getBytes())).toString(16);

        String requestUrl = "https://api.login.yahoo.com" + signatureUrl + "&sig=" + URLEncoder.encode(signature, "UTF-8");

        Document response = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new URL(requestUrl).openStream());
        if (response.getElementsByTagName("Error").getLength() == 0) {
            this.cookie = response.getElementsByTagName("Cookie").item(0).getTextContent();
            this.wssid = response.getElementsByTagName("WSSID").item(0).getTextContent();
        } else {
            throw new AuthException(response.getElementsByTagName("ErrorDescription").item(0).getTextContent());
        }
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new AuthException("Error parsing XML", e);
    } catch (IOException e) {
        throw new AuthException("Communication failure", e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

哪里有问题?我还发现了这个论坛主题:http: //developer.yahoo.com/forum/Yahoo-Mail-Web-Services-API/-Authorization-failed-error-when-using-Yahoo/1304709008000-9792a6b7-5492-3863- a865-ab36576e399f 但这对我没有帮助。

4

0 回答 0