gpt4 book ai didi

java - 我得到 "The Constructor Base64() is not visible error"

转载 作者:太空狗 更新时间:2023-10-29 12:50:35 25 4
gpt4 key购买 nike

我正在尝试从 flicker.com 生成 OAUTH 签名。我从以下位置获得了源代码:

Absolute minimum code to get a valid oauth_signature populated in Java or Groovy?

但是当我尝试执行它时,我收到“The Constructor Base64() is not visible error”。

我的类(class)是:

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();
}



}

最佳答案

如果您使用 Android SDK 中的 Base64 类,则不需要创建对象

http://developer.android.com/reference/android/util/Base64.html#encode(byte[], int)

检查上面的链接,所有编码和解码方法都是静态的,因此您可以在不创建对象的情况下使用它

String s= "hello";    
Base64.encode(s.getBytes());

关于java - 我得到 "The Constructor Base64() is not visible error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12418889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com