android - Flickr API Signing Requests getting "oauth_problem=signature_invalid" -
i trying improve on api connections , figure give flickr go. getting error "oauth_problem=signature_invalid" , not entirely sure error is. think error be; flickr requires utf-8 encoded , using base64 , not entirely sure how utf-8 encoded if issue. possibility "oauth_callback" adding example because not have website. if have , give me feedback on causing "signature_invalid".
https://www.flickr.com/services/api/
the flickr api expects data utf-8 encoded.
results
flickr example: have base string looks following:
get&https%3a%2f%2fwww.flickr.com%2fservices%2foauth%2frequest_token&oauth_callback%3dhttp%253a%252f%252fwww.example.com%26oauth_consumer_key%3d653e7a6ecc1d528c516cc8f92cf98611%26oauth_nonce%3d95613465%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1305586162%26oauth_version%3d1.0
what getting:
get&https%3a%2f%2fwww.flickr.com%2fservices%2foauth%2frequest_token&oauth_callback%3dhttp%253a%252f%252fwww.example.com%26oauth_consumer_key%3d3eec949a9a557a25fdea939f765f49d4%26oauth_nonce%3d112111102120104%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1437587991%26oauth_version%3d1.0
what being displaying in browser:
oauth_problem=signature_invalid&debug_sbs=get&https%3a%2f%2fwww.flickr.com%2fservices%2foauth%2frequest_token&oauth_callback%3dhttp%253a%252f%252fwww.example.com%26oauth_consumer_key%3d3eec949a9a557a25fdea939f765f49d4%26oauth_nonce%3d98115%26oauth_signature_method%3dhmac-sha1%26oauth_timestamp%3d1437587180%26oauth_version%3d1.0
implementation
public class signingrequests { public static void getsigningrequests() { list<string> params = new arraylist<>(arrays.aslist(generateoauthparams())); string[] template = new string[0]; params.add("oauth_signature=" + sign(constants.method_get, constants.method_request_token, params.toarray(template))); try { url url = new url(constants.method_request_token + "?" + paramify(params.toarray(template))); urlconnection api = url.openconnection(); string line; stringbuilder builder = new stringbuilder(); bufferedreader reader = new bufferedreader(new inputstreamreader(api.getinputstream())); while ((line = reader.readline()) != null) builder.append(line); log.e("respinse", line.tostring()); } catch (exception e) { log.e("error", e.tostring()); e.printstacktrace(); } } private static string[] generateoauthparams() { return new string[]{ "oauth_nonce=" + nonce(), "oauth_timestamp=" + time_stamp, "oauth_consumer_key=" + constants.method_key, "oauth_signature_method=hmac-sha1", "oauth_version=1.0", "oauth_callback=http%3a%2f%2fwww.example.com"}; } private static string sign(string method, string uri, string[] params) { string[] p = {method, uri.encode(uri), uri.encode(paramify(params))}; string s = join(p, "&"); secretkey sk = new secretkeyspec(constants.method_secret.getbytes(), "hmacsha1"); try { mac m = mac.getinstance("hmacsha1"); m.init(sk); return uri.encode(new string(base64.encode(m.dofinal(s.getbytes()), base64.default)).trim()); } catch (java.security.nosuchalgorithmexception e) { log.w("fail_alg", e.getmessage()); return null; } catch (java.security.invalidkeyexception e) { log.w("fail_invalid_key", e.getmessage()); return null; } } private static int time_stamp = (int) (new date().gettime() / 1000); private static string nonce() { random r = new random(); stringbuilder n = new stringbuilder(); (int = 0; < r.nextint(8) + 2; i++) n.append(r.nextint(26) + 'a'); return n.tostring(); } private static string paramify(string[] params) { string[] p = arrays.copyof(params, params.length); arrays.sort(p); return join(p, "&"); } private static string join(string[] array, string separator) { stringbuilder b = new stringbuilder(); (int = 0; < array.length; i++) { if (i > 0) b.append(separator); b.append(array[i]); } return b.tostring(); } }
Comments
Post a Comment