我正在尝试从flicker.com生成OAUTH签名.我从以下位置获得了源代码:
Absolute minimum code to get a valid oauth_signature populated in Java or Groovy?
但是,当我尝试执行它时,出现“构造函数Base64()不可见错误”.
我的课是:
public class MainActivity extends Activity { button login; //--------------------------------------------- static String key = "key"; static String secret = "secret"; static final String HMAC_SHA1 = "HmacSHA1"; static final String ENC = "UTF-8"; static Base64 base64 = new Base64(); @SuppressWarnings("static-access") private static String getSignature(String url, String params) throws UnsupportedEnCodingException, NoSuchAlgorithmException, InvalIDKeyException { /** * base has three parts, they are connected by "&": 1) protocol 2) URL * (need to be URLEncoded) 3) Parameter List (need to be URLEncoded). */ 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 = (secret + "&").getBytes(ENC); SecretKey key = new SecretKeySpec(keyBytes, HMAC_SHA1); Mac mac = Mac.getInstance(HMAC_SHA1); mac.init(key); // encode it, base64 it, change it to string and return. return new String(base64.encode(mac.doFinal(base.toString().getBytes( ENC)), 0, 0, 0), ENC).trim(); } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); login=(button)findVIEwByID(R.ID.login_button); login.setonClickListener(new OnClickListener() { public voID onClick(VIEw v) { httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); List<nameValuePair> qparams = new ArrayList<nameValuePair>(); // These params should ordered in key qparams.add(new BasicnameValuePair("oauth_callback", "oob")); qparams.add(new BasicnameValuePair("oauth_consumer_key", 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 = getSignature(URLEncoder.encode( "http://www.flickr.com/services/oauth/request_token", ENC), URLEncoder.encode(URLEncodedUtils.format(qparams, ENC), ENC)); // add it to params List qparams.add(new BasicnameValuePair("oauth_signature", signature)); // generate URI which lead to access_token and token_secret. URI uri = URIUtils.createURI("http", "www.flickr.com", -1, "/services/oauth/request_token", URLEncodedUtils.format(qparams, ENC), null); System.out.println("Get Token and Token Secrect from:" + uri.toString()); httpGet httpget = new httpGet(uri); // output the response content. System.out.println("oken and Token Secrect:"); httpResponse response = httpclIEnt.execute(httpget); httpentity entity = response.getEntity(); if (entity != null) { inputStream instream = entity.getContent(); int len; byte[] tmp = new byte[2048]; while ((len = instream.read(tmp)) != -1) { System.out.println(new String(tmp, 0, len, ENC)); } } } }); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public voID makeAToast(String str) { //yet to implement Toast toast = Toast.makeText(this,str, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); }}
解决方法:
您不能实例化Base64类,因为它的构造函数是私有的.检查源Base64(线号740).您直接提供了静态方法,可以直接将它们作为Base64.encode(string)调用.检查doc
总结以上是内存溢出为你收集整理的java-我收到“构造函数Base64()不可见错误”全部内容,希望文章能够帮你解决java-我收到“构造函数Base64()不可见错误”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)