gpt4 book ai didi

android - Postman Rest Client 如何创建 Oauth 签名?无法在 android 中解析 Oauth_Signature

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:58 25 4
gpt4 key购买 nike

我正在尝试访问 1.0 版本的 Oauth 网络服务。我可以通过 postman 客户端成功完成这项工作,但无法在安卓应用。

使用的库:-路标-commonshttp4-1.2.1.2.jar

API 所需的参数:-oauth_consumer_keyoauth_nonceoauth_versionoauth_signatureoauth_signature_methodoauth_timestamp

代码:-

HttpClient httpclient = new DefaultHttpClient();


// generate the oauth_signature
String urlParamsForSignature = "oauth_consumer_key="+consumerKey +
"&oauth_nonce=" + "pT6c0H"+
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + timestamp +
"&oauth_version=1.0";
String baseString = "https://oauth.withings.com/account/request_token?" + urlParamsForSignature;
String signature = computeHmac(URLEncoder.encode(baseString), consumerSecret);
// add it to params list
qparams.add(new BasicNameValuePair("oauth_signature", signature));

// generate URI which lead to access_token and token_secret.
String urlParams = "oauth_consumer_key="+consumerKey +
"&oauth_nonce=" + "pT6c0H"+
"&oauth_signature=" + signature +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + timestamp +
"&oauth_version=1.0";

String url = "https://oauth.withings.com/account/request_token?" + urlParams;

HttpGet httpget = new HttpGet(url);
// 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));
}
}



public String computeHmac(String baseString, String key)
{
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
return new String(Base64.encodeBase64(digest));
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

最佳答案

根据您发布的代码..您没有使用路标库来生成签名。您正在为此使用您的自定义代码。

您可以按如下方式使用路标库:

//create an oAuth consumer and provide CONSUMER_KEY & CONSUMER SECRET.
DefaultOAuthConsumer defaultOAuthConsumer = new DefaultOAuthConsumer("CONSUMER_KEY","CONSUMER_SECRET");

//REQUEST URL
String url = "https://oauth.withings.com/account/request_token";
try {
// sign the url with consumer. (This will add all oAuth parameters to the query automatically and return the signed request url with all parameter).
url = defaultOAuthConsumer.sign(url);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}

// use the url to make your request.

关于android - Postman Rest Client 如何创建 Oauth 签名?无法在 android 中解析 Oauth_Signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23760525/

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