通过 Play 框架将 yahoo 联系人导入到我的页面时需要帮助

发布于 2024-11-07 01:36:33 字数 6249 浏览 2 评论 0原文

我正在 Play 框架中工作,我正在尝试导入雅虎联系人。

我已经清除了 yahoo 身份验证 api 来获取 access_token 和 guid。

这样,当我尝试使用 http://social.yahooapis.com/v1/user 导入联系人时/{guid}/contacts 与身份验证参数,我在我的页面和日志中收到连接超时异常。

当我将通过代码生成的相同联系网址粘贴到浏览器中时,我得到的签名无效,

我希望我已按照 yahoo api 开发说明中提到的所有内容来创建 oauth_signature,但我仍然没有得到它。

有人可以帮我解决这个问题吗?

用于生成签名的控制器代码 -

public class Yahoo {

 private static String token = "";
 private static String currentUrl = "";
 private static String verifier = "";
 private static String tokenSecret = "";
 private static String accessToken = "";
 private static String yahooGuid = "";


public Yahoo(){
}

/**
 * Requests access to the Yahoo API for request token.
 * @return True if the request is successful, false if not.
 */

public static Yahoo authorize() {
    Session session = Session.current();
    if(session.contains("authorized") && session.get("authorized").equals("0")){
        session.put("authorized", "1");
        String url = getRequestTokenUrl();

        WS.WSRequest request = WS.url(url);
        Logger.info("Yahoo: Create request to get request token'%s'", request.url);

        WS.HttpResponse response = request.get();
        Logger.info("Yahoo: Token response status is %d", response.getStatus());

        if (response.getStatus() == 200){
            String[] pairs = response.getString().split("&");
            String[] tokenSecret = pairs[1].split("=");

            Yahoo.tokenSecret = tokenSecret[1];

            for (String pair : pairs) {
                String[] kv = pair.split("=");
                if (kv.length != 2) {
                    break;
                } else {
                    if (kv[0].equals("oauth_token")) {
                        Yahoo.token = kv[1];
                    }
                }
            }
            Logger.info("level 1 - yahoo token = %s, secret = %s",Yahoo.token, Yahoo.tokenSecret);
        }
    }
    return null;
}


/**
 * Requests access to the Yahoo API for access token.
 * @return True if the request is successful, false if not.
 */

public static Yahoo getAccessToken(){
    String url = getAccessTokenUrl();
    WS.WSRequest request = WS.url(url);
    Logger.info("Yahoo: Create request to get Access token'%s'", request.url);

    WS.HttpResponse response = request.get();
    Logger.info("Yahoo: Token response status is %d", response.getStatus());

    if (response.getStatus() == 200){
        String[] pairs = response.getString().split("&");
        String[] guidPair = pairs[5].split("=");
        String[] tokenSecret = pairs[1].split("=");
        Yahoo.tokenSecret = tokenSecret[1];
        yahooGuid = guidPair[1];
        for (String pair : pairs) {
            String[] kv = pair.split("=");
            if (kv.length != 2) {
                break;
            } else {
                if (kv[0].equals("oauth_token")) {
                    Yahoo.accessToken = kv[1];
                }
            }
        }
        Logger.info("level 3 - yahoo token = %s, secret = %s, guid = %s",Yahoo.accessToken, Yahoo.tokenSecret, Yahoo.yahooGuid);
    }
    return null;
}

/**
 * Requests Signature 
 * @return String 
 */

public static String getBaseSignature(){

    String signature = "";
    String data = generateBaseString();
    String key = keyString();
    Logger.info("key : %s",key);
    try {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        signature = new String(Base64.encode(rawHmac));
        signature = java.net.URLEncoder.encode(signature, "ISO-8859-1");
        Logger.info("Signature=%s", signature);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return signature;
}

/**
 * Requests access to the Yahoo API for getting contacts.
 * 
 */

public static void getContacts(){   
    String url = getContactUrl();
    WS.WSRequest request = WS.url(url);
    Logger.info("Yahoo: Create request to get Contacts '%s'", request.url);

    WS.HttpResponse response = request.get();
    Logger.info("Yahoo: Token response status is %d", response.getStatus());
    if (response.getStatus() == 200){
        String[] pairs = response.getString().split("&");
        for(int i=0;i<pairs.length;i++){
            Logger.info("%s", pairs[i]);
        }
    }else {

        //errors contains a JSON response
        JsonParser parser = new JsonParser();
        JsonObject message = parser.parse(response.getString()).getAsJsonObject();
        Logger.error("Yahoo: Could not get token (status %d): %s", response.getStatus(), message.get("message").getAsString());
    }    
}

public static String generateBaseString(){
    String baseString = getBaseUrl();
    Logger.info("token secret : %s",tokenSecret);
    Logger.info("base url : %s",baseString);
    Logger.info("callback url : %s",getCallBackUrl().toString().split("oauth_token")[0].replace('?', '\0'));
    String returnString = "";
    try {
        returnString = java.net.URLEncoder.encode("GET", "ISO-8859-1") + "&" + java.net.URLEncoder.encode("http://social.yahooapis.com/v1/user/"+yahooGuid+"/contacts", "ISO-8859-1") + "&" + java.net.URLEncoder.encode(baseString, "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Logger.info("Yahoo: Base string: %s",returnString);
    return returnString;
}

public static String keyString(){
    String consumerSecret = encodeString(getConsumerSecret());
    String tokenSec = encodeString(tokenSecret);
    String keyString = consumerSecret + encodeString("&") + tokenSec;
    return keyString;
}

public static String encodeString(String msgString){
    String msg = "";
    try {
        msg = java.net.URLEncoder.encode(msgString.toString(), "ISO-8859-1");
        Logger.info("encode=%s", msg);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return msg;
}

I am working in Play framework and I am trying to import yahoo contacts.

I have cleared the yahoo authentication api's to get the access_token and guid.

With that, when I try to import the contacts using http://social.yahooapis.com/v1/user/{guid}/contacts with the auth parameters, I am getting the connection timeout exception in my page and log.

When I paste the same contact url being generated through the code in the browser, I am getting as signature_invalid

I hope I have followed all the stuffs mentioned in the yahoo api dev notes to create the oauth_signature, but still I am not getting it.

Can anyone help me on this please?

Controller code for generating signature -

public class Yahoo {

 private static String token = "";
 private static String currentUrl = "";
 private static String verifier = "";
 private static String tokenSecret = "";
 private static String accessToken = "";
 private static String yahooGuid = "";


public Yahoo(){
}

/**
 * Requests access to the Yahoo API for request token.
 * @return True if the request is successful, false if not.
 */

public static Yahoo authorize() {
    Session session = Session.current();
    if(session.contains("authorized") && session.get("authorized").equals("0")){
        session.put("authorized", "1");
        String url = getRequestTokenUrl();

        WS.WSRequest request = WS.url(url);
        Logger.info("Yahoo: Create request to get request token'%s'", request.url);

        WS.HttpResponse response = request.get();
        Logger.info("Yahoo: Token response status is %d", response.getStatus());

        if (response.getStatus() == 200){
            String[] pairs = response.getString().split("&");
            String[] tokenSecret = pairs[1].split("=");

            Yahoo.tokenSecret = tokenSecret[1];

            for (String pair : pairs) {
                String[] kv = pair.split("=");
                if (kv.length != 2) {
                    break;
                } else {
                    if (kv[0].equals("oauth_token")) {
                        Yahoo.token = kv[1];
                    }
                }
            }
            Logger.info("level 1 - yahoo token = %s, secret = %s",Yahoo.token, Yahoo.tokenSecret);
        }
    }
    return null;
}


/**
 * Requests access to the Yahoo API for access token.
 * @return True if the request is successful, false if not.
 */

public static Yahoo getAccessToken(){
    String url = getAccessTokenUrl();
    WS.WSRequest request = WS.url(url);
    Logger.info("Yahoo: Create request to get Access token'%s'", request.url);

    WS.HttpResponse response = request.get();
    Logger.info("Yahoo: Token response status is %d", response.getStatus());

    if (response.getStatus() == 200){
        String[] pairs = response.getString().split("&");
        String[] guidPair = pairs[5].split("=");
        String[] tokenSecret = pairs[1].split("=");
        Yahoo.tokenSecret = tokenSecret[1];
        yahooGuid = guidPair[1];
        for (String pair : pairs) {
            String[] kv = pair.split("=");
            if (kv.length != 2) {
                break;
            } else {
                if (kv[0].equals("oauth_token")) {
                    Yahoo.accessToken = kv[1];
                }
            }
        }
        Logger.info("level 3 - yahoo token = %s, secret = %s, guid = %s",Yahoo.accessToken, Yahoo.tokenSecret, Yahoo.yahooGuid);
    }
    return null;
}

/**
 * Requests Signature 
 * @return String 
 */

public static String getBaseSignature(){

    String signature = "";
    String data = generateBaseString();
    String key = keyString();
    Logger.info("key : %s",key);
    try {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        signature = new String(Base64.encode(rawHmac));
        signature = java.net.URLEncoder.encode(signature, "ISO-8859-1");
        Logger.info("Signature=%s", signature);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return signature;
}

/**
 * Requests access to the Yahoo API for getting contacts.
 * 
 */

public static void getContacts(){   
    String url = getContactUrl();
    WS.WSRequest request = WS.url(url);
    Logger.info("Yahoo: Create request to get Contacts '%s'", request.url);

    WS.HttpResponse response = request.get();
    Logger.info("Yahoo: Token response status is %d", response.getStatus());
    if (response.getStatus() == 200){
        String[] pairs = response.getString().split("&");
        for(int i=0;i<pairs.length;i++){
            Logger.info("%s", pairs[i]);
        }
    }else {

        //errors contains a JSON response
        JsonParser parser = new JsonParser();
        JsonObject message = parser.parse(response.getString()).getAsJsonObject();
        Logger.error("Yahoo: Could not get token (status %d): %s", response.getStatus(), message.get("message").getAsString());
    }    
}

public static String generateBaseString(){
    String baseString = getBaseUrl();
    Logger.info("token secret : %s",tokenSecret);
    Logger.info("base url : %s",baseString);
    Logger.info("callback url : %s",getCallBackUrl().toString().split("oauth_token")[0].replace('?', '\0'));
    String returnString = "";
    try {
        returnString = java.net.URLEncoder.encode("GET", "ISO-8859-1") + "&" + java.net.URLEncoder.encode("http://social.yahooapis.com/v1/user/"+yahooGuid+"/contacts", "ISO-8859-1") + "&" + java.net.URLEncoder.encode(baseString, "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Logger.info("Yahoo: Base string: %s",returnString);
    return returnString;
}

public static String keyString(){
    String consumerSecret = encodeString(getConsumerSecret());
    String tokenSec = encodeString(tokenSecret);
    String keyString = consumerSecret + encodeString("&") + tokenSec;
    return keyString;
}

public static String encodeString(String msgString){
    String msg = "";
    try {
        msg = java.net.URLEncoder.encode(msgString.toString(), "ISO-8859-1");
        Logger.info("encode=%s", msg);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return msg;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-11-14 01:36:33

当您在浏览器上复制并粘贴时,您会收到无效签名,因为您没有传递 oauth 标头。

我强烈建议你使用一个库来做到这一点,我已经用 linkedin 做了完全相同的事情:
http://geeks.aretotally.in/projects/play-framework-linkedin-module

您将找到解释和源代码链接。

希望有帮助。

谢谢你,
费利佩

http://geeks.aretotally.in
http://playframework.info
http://mashup.fm

You are getting invalid signature when you copy and paste on the browser because you are not passing the oauth headers.

I strongly suggest you using a lib to do that, I have done the exact same thing with linkedin:
http://geeks.aretotally.in/projects/play-framework-linkedin-module

You will find explanation and link to the source code.

Hope it helps.

Thank you,
Felipe

http://geeks.aretotally.in
http://playframework.info
http://mashup.fm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文