在flickr中创建oauth_signture以获取访问令牌

时间:2013-08-06 07:26:36

标签: oauth flickr

我是flickr API的新手。我获取代码来创建获取请求令牌的签名。但是我无法为访问令牌创建它。总是说签名无效。

我正在使用代码创建签名

private static String getreqSignature(String url, String params)
            throws UnsupportedEncodingException, NoSuchAlgorithmException,
            InvalidKeyException {
    StringBuilder base = new StringBuilder();
            base.append("GET&");
            base.append(url);
            base.append("&");
            base.append(params);
            System.out.println("Stirng for oauth_signature generation:" + base);
            // yea, don't ask me why, it is needed to append a "&" to the end of
            // secret key.
            byte[] keyBytes = (ApplicationContext.getFLICKR_API_SECRET() + "&")
                    .getBytes(ENC);

            SecretKey key = new SecretKeySpec(keyBytes, HMAC_SHA1);

            Mac mac = Mac.getInstance(HMAC_SHA1);
            mac.init(key);
            System.out.println(new String(base64.encode(mac.doFinal(base.toString()
                    .getBytes(ENC))), ENC));
            // encode it, base64 it, change it to string and return.
            return new String(base64.encode(mac.doFinal(base.toString().getBytes(
                    ENC))), ENC).trim();

}

我的查询参数是

qparams.add(new BasicNameValuePair("oauth_consumer_key","******"));
    qparams.add(new BasicNameValuePair("oauth_nonce", ""+ (int) (Math.random()                                                           * 100000000)));
    qparams.add(new BasicNameValuePair("oauth_signature_method","HMAC-SHA1"));
    qparams.add(new BasicNameValuePair("oauth_timestamp", ""+ (System.currentTimeMillis() / 1000)));
    qparams.add(new BasicNameValuePair("oauth_version", "1.0"));

    // generate the oauth_signature
    String signature = getreqSignature(URLEncoder.encode(
            "http://www.flickr.com/services/oauth/request_token", ENC),
            URLEncoder.encode(URLEncodedUtils.format(qparams, ENC), ENC));



    // qparams.add(new BasicNameValuePair("oauth_verifier", verifier));
    qparams.add(new BasicNameValuePair("oauth_signature", signature));

    URI uri = URIUtils.createURI("http", "www.flickr.com", -1,
            "/services/oauth/request_token",
            URLEncodedUtils.format(qparams, ENC), null);

如何创建签名以获取访问令牌。在上面的代码中要更改的内容。

1 个答案:

答案 0 :(得分:0)

最后我使用Scribe库来获取我的详细信息。它工作正常。