gpt4 book ai didi

java - 为什么这个错误: Could not find class 'oauth.路标.http.HttpParameters

转载 作者:行者123 更新时间:2023-12-02 07:51:36 25 4
gpt4 key购买 nike

过去一天我一直在努力为我的 Android 应用程序实现 OAuth。我遇到了一个又一个的问题。我只想签署一个请求,将其发送到网络服务并获得响应(我相信这是两条腿的方法)。

我已经下载了路标:

-signpost-core-1.2.1.2

-signpost-commonshttp4-1.2.1.2

我已将 jar 添加到我的库构建路径中。

我需要在特定的地方放置这些 jar 文件才能使其工作吗?

我遇到最多麻烦的部分是生成签名并最终签署请求。 HttpParemeter 不断抛出错误。

有没有办法让我以编程方式生成 baseString ?

有人可以指导我一个可行的两条腿 Oauth android 特定示例吗?

我的代码如下:

public void excecuteSigning(String targetURL){
String SOAP_ACTION = "http://amebatv.com/api/authDevice";
System.setProperty("http.keepAlive", "false");
HttpURLConnection _request = null;
DefaultHttpClient httpclient = null;

try{

CONSUMER_SECRET = app_pref.getString("consumerSecret", "");
CONSUMER_KEY = app_pref.getString("consumerKey", "");
String oaut_token = app_pref.getString("accessToken", "");
String tokenSecret = app_pref.getString("tokenSecret", "");
String deviceId = deviceInfo.getSerial();


CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer (CONSUMER_KEY,CONSUMER_SECRET);
consumer.setTokenWithSecret(oaut_token, tokenSecret);

//HttpRequest request;
HttpParameters requestParameters = new HttpParameters();
requestParameters.put("file", "vacation.jpg");
requestParameters.put(OAuth.OAUTH_CONSUMER_KEY, "dpf43f3p2l4k3l03");
requestParameters.put(OAuth.OAUTH_NONCE, "kllo9940pd9333jh");
requestParameters.put(OAuth.OAUTH_SIGNATURE_METHOD, "HMAC-SHA1");
requestParameters.put(OAuth.OAUTH_TIMESTAMP, "1191242096");
requestParameters.put(OAuth.OAUTH_TOKEN, "nnch734d00sl2jdk");
requestParameters.put(OAuth.OAUTH_VERSION, "1.0");
requestParameters.put("size", "original");


HttpPost request = new HttpPost(targetURL);
SignatureBaseString baseString = new SignatureBaseString((HttpRequest) request, requestParameters);

String base = baseString.generate();
System.out.println(":"+base);

System.out.println(computeHmac(base,"kd94hf93k423kf44&pfkkdhi9sl3r4s00"));

httpclient = new DefaultHttpClient();

ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer writer = new OutputStreamWriter(outstream, "UTF-8");
writeXml(writer);
}
};

HttpParameters params = new HttpParameters();
HttpEntity entity = new EntityTemplate(cp);
HttpPost request = new HttpPost(targetURL);

request.addHeader("Authorization",AUTH_HEADER);
request.addHeader("Content-Type", "text/xml; charset=utf-8");
//request.addHeader("Content-Length",""+soapXML.getBytes().length);
request.addHeader("SOAPAction",SOAP_ACTION);
request.setEntity(entity);

// sign the request
consumer.sign(request);
// send the request
//request.connect();
HttpResponse response = httpclient.execute(request);

//get response
//StringBuffer response = new StringBuffer();
InputStream instream = null;
BufferedReader br = null;
try{
int respcode = response.getStatusLine().getStatusCode();
Log.i("Server Response", ""+respcode);

//get XML from InputStream
if(respcode>= 200){
instream = response.getEntity().getContent();
client.buildDoc(instream);
}
else{
Log.i("Server Response", ""+respcode);
//instream = request.getErrorStream();
}

}catch(Exception e){
Log.i("SPLASH","Unable to create connection stream");
e.printStackTrace();
}
finally {
if(instream != null) {
Log.i("SPLASH","Disconnecting stream");
instream.close();
}
}
*/
}
catch(Exception e){
Log.i("SPLASH","Unable to create connection");
e.printStackTrace();
}finally {
if(httpclient != null) {
Log.i("SPLASH","Disconnecting");
httpclient.getConnectionManager().shutdown();
}
}
}

/*
* Method:computeHmac()
* @params: String, String
* return: String
*/
public String computeHmac(String baseString, String key)
throws NoSuchAlgorithmException,
InvalidKeyException,
IllegalStateException,
UnsupportedEncodingException
{

Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
return Base64.encodeToString(digest,Base64.URL_SAFE);
}

最佳答案

库需要位于 libs 文件夹中(带有 s!),然后它们会自动添加到项目的构建路径中。

关于java - 为什么这个错误: Could not find class 'oauth.路标.http.HttpParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10164622/

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