Ошибка интеграции Blackberry linkedin при получении OAuth Token - PullRequest
1 голос
/ 23 февраля 2012

Я пытаюсь интегрировать Linkedin в Blackberry.Я следовал инструкциям Linkedin по Получение OAuth-токена .

Я успешно получил токен запроса.После этого я отправляю запрос на получение токена доступа.Затем отображается код ошибки 401. Как устранить ошибку?

oauth_problem=signature_invalid&oauth_problem_advice=com.linkedin.security.auth.pub.LoginDeniedInvalidAuthTokenException%20while%20obtaining%20request%20token%20for%20%3APOST%26https%253A%252F%252Fapi.linkedin.com%252Fuas%252Foauth%252FaccessToken%26oauth_consumer_key%ffffffffffff%2526oauth_nonce%253D8928851959000826448%2526oauth_signature_method%253DHMAC-SHA1%2526oauth_timestamp%253D1329997670%2526oauth_token%253Dc98f4ecf-26b5-44aa-a11f-8384ecdacac0%2526oauth_verifier%253D85932%2526oauth_version%253D1.0%0AOAU%3Adkkyeudmhykb%7Cc98f4ecf-26b5-44aa-a11f-8384ecdacac0%7C%2A01%7C%2A01%3A1329997670%3AUDYg4juNLVHzmP7vk%2FRSfhihLlI%3DAccess tockenem=signature_invalid

Совет по проблеме декодируется в

com.linkedin.security.auth.pub.LoginDeniedInvalidAuthTokenException while obtaining request token for :POST&https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2FaccessToken&oauth_consumer_key▒ffffffffff%26oauth_nonce%3D8928851959000826448%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1329997670%26oauth_token%3Dc98f4ecf-26b5-44aa-a11f-8384ecdacac0%26oauth_verifier%3D85932%26oauth_version%3D1.0OAU:dkkyeudmhykb|c98f4ecf-26b5-44aa-a11f-8384ecdacac0|*01|*01:1329997670:UDYg4juNLVHzmP7vk/RSfhihLlI=Access tockenem=signature_invalid

, что декодирует встроенный URL-адрес

https://api.linkedin.com/uas/oauth/accessToken&oauth_consumer_key▒ffffffffff&oauth_nonce=8928851959000826448&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1329997670&oauth_token=c98f4ecf-26b5-44aa-a11f-8384ecdacac0&oauth_verifier=85932&oauth_version=1.0OAU:dkkyeudmhykb|c98f4ecf-26b5-44aa-a11f-8384ecdacac0|*01|*01:1329997670:UDYg4juNLVHzmP7vk/RSfhihLlI=

Мой код -

    InputStream input = null;
    HttpConnection connection = null;
    String url = "https://api.linkedin.com/uas/oauth/accessToken";
    if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) && RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
        url += ";interface=wifi";
        }
    String header = oauth_header1(url, HttpProtocolConstants.HTTP_METHOD_POST);
    requestTokenUrl = concatURL(url, header);
    Dialog.alert("=============="+requestTokenUrl);
    try{
        connection = (HttpConnection) Connector.open(requestTokenUrl);
        connection.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_POST);
        input = connection.openDataInputStream();
        int resp = connection.getResponseCode();
        Dialog.alert(resp+"");
        if (resp == HttpConnection.HTTP_OK) {
            StringBuffer buffer = new StringBuffer();
            int ch;
            while ( (ch = input.read()) != -1){
                buffer.append( (char) ch);
            }
            String content = buffer.toString();
            Dialog.alert(content);
            }

public static String oauth_header1(String url, String method) {
    String nonce = nonce();
    long timestamp = timestamp();
    Const.nonce=nonce;
    Const.time=Long.toString(timestamp);
    Hashtable pairs = new Hashtable();
     String ll=Const.OAUTH_TOKEN_val;


    pairs.put("oauth_consumer_key",Const.consumerKey); 
    pairs.put("oauth_nonce",nonce); 
    pairs.put("oauth_signature_method",Const.SIGNATURE_METHOD); 
    pairs.put("oauth_timestamp",Long.toString(timestamp)); 
    pairs.put("oauth_token",ll); 
    pairs.put("oauth_verifier",Const.ver); 
    pairs.put("oauth_version","1.0"); 


    String sig1 = signature(method, url, pairs);

   //http://linkedin_oauth/success
    StringBuffer header_sb = new StringBuffer();
    header_sb.append("oauth_consumer_key=").append(URLUTF8Encoder.encode(Const.consumerKey)).append("&");
    header_sb.append("oauth_nonce=").append(URLUTF8Encoder.encode(nonce)).append("&");
    header_sb.append("oauth_signature_method=").append(URLUTF8Encoder.encode(Const.SIGNATURE_METHOD)).append("&");
    header_sb.append("oauth_signature=").append(URLUTF8Encoder.encode(sig1)).append("&");
    header_sb.append("oauth_timestamp=").append(Long.toString(timestamp)).append("&");
    header_sb.append("oauth_token=").append(URLUTF8Encoder.encode(ll)).append("&");
    header_sb.append("oauth_verifier=").append(URLUTF8Encoder.encode(Const.ver)).append("&");
    header_sb.append("oauth_version=").append(URLUTF8Encoder.encode("1.0"));


    return header_sb.toString();

}   




private static String signature(String method, String requestURL, Hashtable pairs) {
    StringBuffer sb = new StringBuffer();
    String[] keys = new String[pairs.size()];       
    Enumeration e = pairs.keys();
    int i = 0;

    while(e.hasMoreElements()) {
        String k = (String)e.nextElement();
        keys[i++] = k + "=" + URLUTF8Encoder.encode((String)pairs.get(k));
    }
    Arrays.sort(keys, new Comparator() {
        public int compare(Object arg0, Object arg1) {
            return ((String)arg0).compareTo((String)arg1);
        }
    });
    for(i = 0; i < keys.length; i++) {
        sb.append(keys[i]).append('&');
    }
    sb.deleteCharAt(sb.length()-1);
    String msg = method.toUpperCase() +"&" + URLUTF8Encoder.encode(requestURL) + "&" + URLUTF8Encoder.encode(sb.toString());
    System.out.println(msg);

    StringBuffer key = new StringBuffer();
    if(Const.consumerSecret != null) key.append(URLUTF8Encoder.encode(Const.consumerSecret));
    key.append('&');

    if(Const.tokenSecret != null){
        key.append(URLUTF8Encoder.encode(Const.tokenSecret));
    }

    try {
        return hmacsha1(key.toString(), msg);
    } catch (Exception ex) {
        return null;
    }
}


public static String concatURL(String url, String header){
    String newurl=url;
    header = header.replace(',', '&');
    newurl = newurl+"?"+header;
    return newurl;
}






private static String hmacsha1(String key, String message)
throws CryptoTokenException, CryptoUnsupportedOperationException, IOException {
    HMACKey k = new HMACKey(key.getBytes());
    HMAC hmac = new HMAC(k, new SHA1Digest());
    hmac.update(message.getBytes());
    byte[] mac = hmac.getMAC();
    return Base64OutputStream.encodeAsString(mac, 0, mac.length, false, false);
}
...